0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RubyでAtCoder ABC304(A, B, C)を解いてみた

Posted at

はじめに

Webエンジニアを目指して、RubyやRailsをいじってます。
今回は、RubyでAtCoder ABC304のA, B, Cを解きました。備忘録として解き方をまとめていきたいと思います。

A - First Player

a-304.rb
n = gets.to_i

name_group, age_group = [], [] 
n.times do
  s, a = gets.split
  name_group << s
  age_group  << a.to_i
end

start_point = age_group.index(age_group.min)
start_point.times{ name_group << name_group.shift }
puts name_group

解説

最少年齢の人のインデックスを求め、その回数分name_groupの先頭にある名前を末尾に移動させることで解くことができます。

B - Subscribers

b-304.rb
str = gets.chomp
n = str.size
puts n <= 3 ? str : str[0..2] + ("0" * (n - 3))

C - Virus

c-304.rb
n, d = gets.split.map(&:to_i)
points = Array.new(n){ gets.split.map(&:to_i) }

array = Array.new(n, false)
array[0] = true
stack = [0]
while node = stack.pop
  x, y = points[node]
  points.each_with_index do |(nx, ny), i|
    next if array[i] || (x - nx) ** 2 + (y - ny) ** 2 > d ** 2
    stack << i
    array[i] = true
  end
end
puts array.map{ |infected| infected ? "Yes" : "No" }

解説

DFSにより、それぞれの人に対して感染しているかどうかを探索することができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?