結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー 小指が強い人小指が強い人
提出日時 2015-11-16 18:48:53
言語 Ruby
(3.3.0)
結果
AC  
実行時間 85 ms / 5,000 ms
コード長 937 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 11,464 KB
実行使用メモリ 15,272 KB
最終ジャッジ日時 2023-08-27 00:09:10
合計ジャッジ時間 3,148 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
15,092 KB
testcase_01 AC 82 ms
15,264 KB
testcase_02 AC 83 ms
15,116 KB
testcase_03 AC 82 ms
15,120 KB
testcase_04 AC 84 ms
15,064 KB
testcase_05 AC 82 ms
15,172 KB
testcase_06 AC 82 ms
14,960 KB
testcase_07 AC 83 ms
15,244 KB
testcase_08 AC 83 ms
15,240 KB
testcase_09 AC 84 ms
15,056 KB
testcase_10 AC 83 ms
15,136 KB
testcase_11 AC 84 ms
15,272 KB
testcase_12 AC 84 ms
15,064 KB
testcase_13 AC 84 ms
15,076 KB
testcase_14 AC 83 ms
15,076 KB
testcase_15 AC 84 ms
14,964 KB
testcase_16 AC 83 ms
15,072 KB
testcase_17 AC 84 ms
15,060 KB
testcase_18 AC 84 ms
15,248 KB
testcase_19 AC 82 ms
15,044 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = 4
$a = Array.new(N)
$b = Array.new(N).map{ Array.new(N, true) }
$dx = [1, 0, -1, 0]
$dy = [0, 1, 0, -1]
def process(cx, cy, n)
    if n == 0
        return true
    end
    v = cy * N + cx + 1
    4.times do |i|
        x = cx + $dx[i]
        y = cy + $dy[i]
        if x < 0 || x >= N || y < 0 || y >= N
            next
        elsif !$b[y][x]
            next
        elsif v != $a[y][x]
            next
        end
        $b[y][x] = false
        if process(x, y, n - 1)
            return true
        end
        $b[y][x] = true
    end
    return false
end
fx = 0
fy = 0
n = 0
N.times do |i|
    $a[i] = gets.split.map(&:to_i)
    N.times do |j|
        if $a[i][j] == i * N + j + 1
            $b[i][j] = false
        elsif $a[i][j] == 0
            fx = j
            fy = i
            $b[i][j] = false
        else
            n += 1
        end
    end
end
if process(fx, fy, n)
    puts "Yes"
else
    puts "No"
end
0