結果
問題 |
No.157 2つの空洞
|
ユーザー |
![]() |
提出日時 | 2015-04-03 07:42:58 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 113 ms / 2,000 ms |
コード長 | 1,630 bytes |
コンパイル時間 | 39 ms |
コンパイル使用メモリ | 7,296 KB |
実行使用メモリ | 12,544 KB |
最終ジャッジ日時 | 2024-07-04 01:36:35 |
合計ジャッジ時間 | 2,674 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 16 |
コンパイルメッセージ
Syntax OK
ソースコード
class Yukicoder attr_accessor :field, :width, :height DX = [1, 0, -1, 0] DY = [0, 1, 0, -1] def initialize @width, @height = gets.chomp.split(' ').map(&:to_i) @field = [] height.times do |y| @field << gets.chomp.split('') end id = 0 height.times do |y| width.times do |x| if field[y][x] == '.' bfs(y, x, id) id += 1 end end end min_dist = Float::INFINITY height.times do |y| width.times do |x| if field[y][x] == '0' min_dist = [min_dist, calc_dist(y, x)].min end end end puts min_dist end def calc_dist(y, x) coords = [[y,x,0]] check_list = Hash.new(false) while coords.any? y, x, dist = coords.shift return dist - 1 if field[y][x] == '1' next if check_list[y * width + x] check_list[y * width + x] = true 4.times do |i| ny = y + DY[i] nx = x + DX[i] if is_wall?(ny, nx) && field[ny][nx] != '0' coords << [ny, nx, dist + 1] end end end Float::INFINITY end def is_wall?(y, x) 0 <= y && y < height && 0 <= x && x < width end def bfs(y, x, id) coords = [[y,x]] check_list = Hash.new(false) while coords.any? y, x = coords.shift next if check_list[y * width + x] check_list[y * width + x] = true field[y][x] = id.to_s 4.times do |i| ny = y + DY[i] nx = x + DX[i] if is_wall?(ny, nx) && field[ny][nx] == '.' coords << [ny, nx] end end end end end Yukicoder.new