結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー lam6er
提出日時 2025-03-20 20:37:27
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,720 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 82,792 KB
実行使用メモリ 54,320 KB
最終ジャッジ日時 2025-03-20 20:38:16
合計ジャッジ時間 1,679 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    # Read the input
    grid = []
    for _ in range(4):
        grid.extend(list(map(int, input().split())))
    
    # Determine the initial positions (original puzzle state)
    initial_positions = {1: (0,0), 2: (0,1), 3: (0,2), 4: (0,3),
                         5: (1,0), 6: (1,1), 7: (1,2), 8: (1,3),
                         9: (2,0), 10: (2,1), 11: (2,2), 12: (2,3),
                         13: (3,0), 14: (3,1), 15: (3,2), 0: (3,3)}
    
    # Check condition 1: Manhattan distance for each tile except 0 must be 0 or 1
    for num in range(1, 16):
        idx = grid.index(num)
        target_row = idx // 4
        target_col = idx % 4
        initial_row, initial_col = initial_positions[num]
        dx = abs(target_row - initial_row)
        dy = abs(target_col - initial_col)
        if dx + dy > 1:
            print("No")
            return
    
    # Check solvability under classic rules
    
    # For the solvability, we need to compute the parity of the inversion count and the blank row movement
    flat = [num for num in grid if num != 0]
    inv_count = 0
    for i in range(len(flat)):
        for j in range(i + 1, len(flat)):
            if flat[i] > flat[j]:
                inv_count += 1
    
    # Find the position of 0 (blank) in the initial and target grids
    initial_blank_row = 3  # 0 is at (3,3) in initial
    target_blank_row = 0
    for i in range(16):
        if grid[i] == 0:
            target_blank_row = i // 4
    
    # Calculate the row difference for the blank tile
    row_diff = target_blank_row - initial_blank_row
    if (inv_count + row_diff) % 2 != 0:
        print("No")
        return
    
    print("Yes")

if __name__ == "__main__":
    main()
0