結果
問題 | No.2731 Two Colors |
ユーザー | 👑 KA37RI |
提出日時 | 2024-04-19 22:31:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 936 ms / 3,000 ms |
コード長 | 1,729 bytes |
コンパイル時間 | 850 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 103,856 KB |
最終ジャッジ日時 | 2024-10-11 16:06:48 |
合計ジャッジ時間 | 14,505 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 897 ms
93,012 KB |
testcase_01 | AC | 936 ms
93,312 KB |
testcase_02 | AC | 919 ms
93,148 KB |
testcase_03 | AC | 128 ms
76,928 KB |
testcase_04 | AC | 168 ms
77,528 KB |
testcase_05 | AC | 183 ms
77,652 KB |
testcase_06 | AC | 224 ms
80,732 KB |
testcase_07 | AC | 204 ms
80,608 KB |
testcase_08 | AC | 160 ms
77,396 KB |
testcase_09 | AC | 326 ms
92,592 KB |
testcase_10 | AC | 149 ms
77,596 KB |
testcase_11 | AC | 441 ms
101,028 KB |
testcase_12 | AC | 330 ms
92,936 KB |
testcase_13 | AC | 391 ms
97,036 KB |
testcase_14 | AC | 265 ms
84,312 KB |
testcase_15 | AC | 182 ms
77,216 KB |
testcase_16 | AC | 290 ms
85,400 KB |
testcase_17 | AC | 260 ms
87,432 KB |
testcase_18 | AC | 192 ms
78,816 KB |
testcase_19 | AC | 292 ms
86,652 KB |
testcase_20 | AC | 255 ms
85,764 KB |
testcase_21 | AC | 193 ms
78,364 KB |
testcase_22 | AC | 401 ms
96,684 KB |
testcase_23 | AC | 232 ms
80,976 KB |
testcase_24 | AC | 199 ms
78,780 KB |
testcase_25 | AC | 121 ms
76,184 KB |
testcase_26 | AC | 299 ms
89,960 KB |
testcase_27 | AC | 350 ms
94,896 KB |
testcase_28 | AC | 314 ms
90,040 KB |
testcase_29 | AC | 224 ms
81,356 KB |
testcase_30 | AC | 236 ms
82,728 KB |
testcase_31 | AC | 225 ms
80,984 KB |
testcase_32 | AC | 433 ms
103,856 KB |
testcase_33 | AC | 42 ms
52,736 KB |
testcase_34 | AC | 44 ms
52,608 KB |
testcase_35 | AC | 43 ms
52,864 KB |
ソースコード
import heapq bit10 = (1 << 10) - 1 def solve(h, w, a): col = [[-1] * w for _ in range(h)] ans = 0 num = (a[0][0] << 20) | 0 queue0 = [num] num = (a[h-1][w-1] << 20) | (h-1 << 10) | w-1 queue1 = [num] while True: ans += 1 filled = False while True: if queue0: num = heapq.heappop(queue0) i = (num >> 10) & bit10 j = num & bit10 if col[i][j] != -1: continue filled = True col[i][j] = 0 for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]: ni, nj = i + di, j + dj if ni < 0 or ni >= h or nj < 0 or nj >= w: continue if col[ni][nj] == 0: continue elif col[ni][nj] == 1: return ans - 2 else: num = (a[ni][nj] << 20) | (ni << 10) | nj heapq.heappush(queue0, num) if filled: break ans += 1 while True: if queue1: num = heapq.heappop(queue1) i = (num >> 10) & bit10 j = num & bit10 if col[i][j] != -1: continue filled = True col[i][j] = 1 for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]: ni, nj = i + di, j + dj if ni < 0 or ni >= h or nj < 0 or nj >= w: continue if col[ni][nj] == 1: continue elif col[ni][nj] == 0: return ans - 2 else: num = (a[ni][nj] << 20) | (ni << 10) | nj heapq.heappush(queue1, num) if filled: break def main(): h, w = [int(x) for x in input().split()] a = [[int(x) for x in input().split()] for _ in range(h)] print(solve(h, w, a)) if __name__ == "__main__": main()