結果

問題 No.157 2つの空洞
ユーザー 小指が強い人小指が強い人
提出日時 2015-11-23 16:48:05
言語 Ruby
(3.3.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 1,395 bytes
コンパイル時間 59 ms
コンパイル使用メモリ 11,312 KB
実行使用メモリ 15,512 KB
最終ジャッジ日時 2023-10-11 18:22:30
合計ジャッジ時間 2,904 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,300 KB
testcase_01 AC 77 ms
15,048 KB
testcase_02 AC 79 ms
15,124 KB
testcase_03 AC 77 ms
15,264 KB
testcase_04 AC 79 ms
15,076 KB
testcase_05 AC 79 ms
15,148 KB
testcase_06 AC 80 ms
15,148 KB
testcase_07 AC 78 ms
15,336 KB
testcase_08 AC 79 ms
15,316 KB
testcase_09 AC 80 ms
15,076 KB
testcase_10 AC 78 ms
15,260 KB
testcase_11 AC 78 ms
15,028 KB
testcase_12 AC 78 ms
15,016 KB
testcase_13 AC 78 ms
15,124 KB
testcase_14 AC 80 ms
15,300 KB
testcase_15 AC 80 ms
15,092 KB
testcase_16 AC 82 ms
15,136 KB
testcase_17 AC 82 ms
15,512 KB
testcase_18 AC 81 ms
15,060 KB
testcase_19 AC 82 ms
15,076 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:1: warning: possibly useless use of a variable in void context
Main.rb:2: warning: possibly useless use of a variable in void context
Main.rb:3: warning: possibly useless use of a variable in void context
Main.rb:4: warning: possibly useless use of a variable in void context
Main.rb:5: warning: possibly useless use of a variable in void context
Main.rb:42: warning: assigned but unused variable - d
Syntax OK

ソースコード

diff #

$w
$h
$a
$b
$c
$dx = [1, 0, -1, 0]
$dy = [0, 1, 0, -1]
$min = 10 ** 10
def checkRange(x, y)
    return x >= 0 && x < $w && y >= 0 && y < $h
end
def paint(cx, cy, v)
    $b[cy][cx] = true
    if $c[cy][cx] == "#"
        return
    end
    $a[cy][cx] = v
    4.times do |i|
        tx = cx + $dx[i]
        ty = cy + $dy[i]
        next if !checkRange(tx, ty) || $b[ty][tx]
        paint(tx, ty, v)
    end
end
$w, $h = gets.split.map(&:to_i)
$a = Array.new($h).map{ Array.new($w, 0) }
$b = Array.new($h).map{ Array.new($w, false) }
$c = Array.new($h)
$h.times do |i|
    $c[i] = gets.strip
end
v = 1
$h.times do |i|
    $w.times do |j|
        if $c[i][j] == "." && $a[i][j] == 0
            paint(j, i, v)
            v += 1
        end
    end
end
coord = Struct.new(:x, :y)
d = Array.new($h).map{ Array.new($w, false) }
ah = Array.new
bh = Array.new
$h.times do |i|
    $w.times do |j|
        if $a[i][j] != 0
            next
        end
        4.times do |k|
            tx = j + $dx[k]
            ty = i + $dy[k]
            next if !checkRange(tx, ty)
            if $a[ty][tx] == 1
                ah.push(coord.new(j, i))
            elsif $a[ty][tx] == 2
                bh.push(coord.new(j, i))
            end
        end
    end
end
min = 10 ** 10
ah.each do |a|
    bh.each do |b|
        v = (a.x - b.x).abs + (a.y - b.y).abs + 1
        min = v if v < min
    end
end
puts min
0