結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー code-devocode-devo
提出日時 2016-01-21 11:08:11
言語 Ruby
(3.3.0)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 769 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 11,432 KB
実行使用メモリ 15,568 KB
最終ジャッジ日時 2023-08-27 06:11:36
合計ジャッジ時間 2,880 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,332 KB
testcase_01 AC 74 ms
15,220 KB
testcase_02 AC 77 ms
15,220 KB
testcase_03 AC 84 ms
15,444 KB
testcase_04 AC 76 ms
15,176 KB
testcase_05 AC 89 ms
15,096 KB
testcase_06 AC 81 ms
15,188 KB
testcase_07 AC 83 ms
15,340 KB
testcase_08 AC 79 ms
15,228 KB
testcase_09 AC 85 ms
15,412 KB
testcase_10 AC 78 ms
15,072 KB
testcase_11 AC 81 ms
15,568 KB
testcase_12 AC 83 ms
15,284 KB
testcase_13 AC 83 ms
15,484 KB
testcase_14 AC 81 ms
15,336 KB
testcase_15 AC 83 ms
15,476 KB
testcase_16 AC 85 ms
15,480 KB
testcase_17 AC 88 ms
15,368 KB
testcase_18 AC 84 ms
15,528 KB
testcase_19 AC 82 ms
15,528 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# うーん・・・

T = $stdin.read.lines.map{|s| s.split.map(&:to_i)}
W, H = T[0].size, T.size

OFFSETS = [[-1, 0], [+1, 0], [0, -1], [0, +1]]

def check(map, x, y, a)
  return true if map == T

  OFFSETS.each do |ox, oy|
    new_x = x + ox
    new_y = y + oy
    if 0 <= new_x && new_x < W && 0 <= new_y && new_y < H then
      n = map[new_y][new_x]
      unless a[n] then
        new_map = map.map{|r| r.dup}
        new_map[y][x] = n
        new_map[new_y][new_x] = 0
        new_a = a.dup
        new_a[n] = true
        return true if check(new_map, new_x, new_y, new_a)
      end
    end
  end

  return false
end

map = Array.new(H){|i| Array.new(W){|j| i * H + j + 1}}
x, y = W - 1, H - 1
map[y][x] = 0
puts check(map, x, y, Array.new(W * H)) ? "Yes" : "No"
0