結果
| 問題 |
No.228 ゆきこちゃんの 15 パズル
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-31 17:46:35 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,562 bytes |
| コンパイル時間 | 445 ms |
| コンパイル使用メモリ | 82,220 KB |
| 実行使用メモリ | 54,228 KB |
| 最終ジャッジ日時 | 2025-03-31 17:47:13 |
| 合計ジャッジ時間 | 1,760 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 WA * 1 |
ソースコード
def main():
# Read input
grid = []
for _ in range(4):
grid.append(list(map(int, input().split())))
# Determine target positions and find the blank (0) position
target_positions = {}
blank_pos = None
for i in range(4):
for j in range(4):
num = grid[i][j]
if num == 0:
blank_pos = (i + 1, j + 1) # Convert to 1-based index
else:
target_positions[num] = (i + 1, j + 1) # Store as 1-based
# Condition 1: Solvability check (parity of inversion count and blank row difference)
# Compute inversion count of target (excluding 0)
flattened = []
for row in grid:
for num in row:
if num != 0:
flattened.append(num)
inversion = 0
for i in range(len(flattened)):
for j in range(i + 1, len(flattened)):
if flattened[i] > flattened[j]:
inversion += 1
initial_blank_row = 4
target_blank_row = blank_pos[0]
row_diff = abs(initial_blank_row - target_blank_row)
condition1 = (inversion % 2) == (row_diff % 2)
if not condition1:
print("No")
return
# Condition 2: All manhattan distances between initial and target positions are 0 or 1
condition2 = True
for num in range(1, 16): # Numbers 1 to 15
# Initial position of num is based on standard initial 15-puzzle
initial_i = (num - 1) // 4 + 1
initial_j = (num - 1) % 4 + 1
target_pos = target_positions.get(num)
if target_pos is None:
# This shouldn't happen as per the problem statement
print("No")
return
dx = abs(initial_i - target_pos[0])
dy = abs(initial_j - target_pos[1])
if dx + dy > 1:
condition2 = False
break
if not condition2:
print("No")
return
# Condition3: D and K must have the same parity
# Compute D (Manhattan distance of blank's movement)
initial_blank = (4, 4)
D = abs(initial_blank[0] - blank_pos[0]) + abs(initial_blank[1] - blank_pos[1])
# Compute K (number of moved tiles)
K = 0
for num in range(1, 16):
initial_i = (num - 1) // 4 + 1
initial_j = (num - 1) % 4 + 1
target_i, target_j = target_positions[num]
if initial_i != target_i or initial_j != target_j:
K += 1
if (D % 2) != (K % 2):
print("No")
return
print("Yes")
if __name__ == "__main__":
main()
lam6er