結果
問題 | No.2731 Two Colors |
ユーザー |
|
提出日時 | 2024-04-20 16:01:03 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,601 ms / 3,000 ms |
コード長 | 1,578 bytes |
コンパイル時間 | 427 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 120,232 KB |
最終ジャッジ日時 | 2024-10-12 11:44:31 |
合計ジャッジ時間 | 20,520 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
from heapq import heappop, heappushH, W = map(int, input().split())A = [list(map(int, input().split())) for _ in range(H)]di, dj = [1, 0, -1, 0], [0, 1, 0, -1]hq1 = []hq2 = []C = [[-1] * W for _ in range(H)]C[0][0] = 0C[-1][-1] = 1add1 = [[False] * W for _ in range(H)]add2 = [[False] * W for _ in range(H)]for d in range(4):ni, nj = di[d], dj[d]if 0 <= ni < H and 0 <= nj < W:add1[ni][nj] = Trueheappush(hq1, (A[ni][nj], ni, nj))for d in range(4):ni, nj = H - 1 + di[d], W - 1 + dj[d]if 0 <= ni < H and 0 <= nj < W:add2[ni][nj] = Trueheappush(hq2, (A[ni][nj], ni, nj))ans = 1while hq1 and hq2:if ans & 1:v, i, j = heappop(hq1)C[i][j] = 0for d in range(4):ni, nj = i + di[d], j + dj[d]if 0 <= ni < H and 0 <= nj < W and C[ni][nj] != -1 and C[ni][nj] != C[i][j]:print(ans)exit()if 0 <= ni < H and 0 <= nj < W and C[ni][nj] == -1 and not add1[ni][nj]:add1[ni][nj] = Trueheappush(hq1, (A[ni][nj], ni, nj))else:v, i, j = heappop(hq2)C[i][j] = 1for d in range(4):ni, nj = i + di[d], j + dj[d]if 0 <= ni < H and 0 <= nj < W and C[ni][nj] != -1 and C[ni][nj] != C[i][j]:print(ans)exit()if 0 <= ni < H and 0 <= nj < W and C[ni][nj] == -1 and not add2[ni][nj]:add2[ni][nj] = Trueheappush(hq2, (A[ni][nj], ni, nj))ans += 1assert 0