結果
| 問題 | No.228 ゆきこちゃんの 15 パズル | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-01-21 11:08:11 | 
| 言語 | Ruby (3.4.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 109 ms / 5,000 ms | 
| コード長 | 769 bytes | 
| コンパイル時間 | 312 ms | 
| コンパイル使用メモリ | 7,296 KB | 
| 実行使用メモリ | 12,416 KB | 
| 最終ジャッジ日時 | 2024-12-26 16:41:31 | 
| 合計ジャッジ時間 | 3,331 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
コンパイルメッセージ
Syntax OK
ソースコード
# うーん・・・
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"
            
            
            
        