結果

問題 No.2731 Two Colors
ユーザー hato336hato336
提出日時 2024-04-19 21:39:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,401 ms / 3,000 ms
コード長 1,284 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 275,784 KB
最終ジャッジ日時 2024-04-19 21:39:54
合計ジャッジ時間 24,199 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,376 ms
275,500 KB
testcase_01 AC 2,371 ms
275,784 KB
testcase_02 AC 2,401 ms
275,356 KB
testcase_03 AC 191 ms
90,524 KB
testcase_04 AC 195 ms
91,584 KB
testcase_05 AC 203 ms
91,736 KB
testcase_06 AC 294 ms
99,472 KB
testcase_07 AC 252 ms
99,212 KB
testcase_08 AC 187 ms
90,872 KB
testcase_09 AC 525 ms
123,536 KB
testcase_10 AC 168 ms
91,000 KB
testcase_11 AC 978 ms
164,388 KB
testcase_12 AC 493 ms
121,244 KB
testcase_13 AC 851 ms
146,944 KB
testcase_14 AC 348 ms
106,056 KB
testcase_15 AC 191 ms
91,044 KB
testcase_16 AC 446 ms
114,512 KB
testcase_17 AC 489 ms
116,820 KB
testcase_18 AC 238 ms
94,812 KB
testcase_19 AC 472 ms
115,392 KB
testcase_20 AC 374 ms
105,672 KB
testcase_21 AC 240 ms
94,972 KB
testcase_22 AC 683 ms
137,512 KB
testcase_23 AC 341 ms
101,000 KB
testcase_24 AC 238 ms
95,204 KB
testcase_25 AC 168 ms
90,544 KB
testcase_26 AC 500 ms
119,148 KB
testcase_27 AC 659 ms
133,236 KB
testcase_28 AC 578 ms
128,780 KB
testcase_29 AC 312 ms
99,296 KB
testcase_30 AC 345 ms
103,312 KB
testcase_31 AC 335 ms
102,380 KB
testcase_32 AC 952 ms
162,148 KB
testcase_33 AC 109 ms
85,604 KB
testcase_34 AC 109 ms
85,300 KB
testcase_35 AC 109 ms
85,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
#input = sys.stdin.readline
#n = int(input())
#
#alist = []
#s = input()
h,w = map(int,input().split())
grid = [list(map(int,input().split())) for i in range(h)]
color = [[],[]]
s = [set(),set()]
c = [[-1 for i in range(w)] for j in range(h)]
c[0][0] = 0
c[h-1][w-1] = 1
heapq.heappush(color[0],(grid[0][1],0,1))
heapq.heappush(color[0],(grid[1][0],1,0))
s[0].add((0,1))
s[0].add((1,0))
s[0].add((0,0))
heapq.heappush(color[1],(grid[h-2][w-1],h-2,w-1))
heapq.heappush(color[1],(grid[h-1][w-2],h-1,w-2))
s[1].add((h-2,w-1))
s[1].add((h-1,w-2))
s[1].add((h-1,w-1))
ans = 0
dxy = [[-1,0],[1,0],[0,1],[0,-1]]
for i in range(h*w):
    v,x,y = heapq.heappop(color[i%2])
    while c[x][y] != -1:
        if not color[i%2]:
            print(ans)
            exit()
        v,x,y = heapq.heappop(color[i%2])
    c[x][y] = i%2
    ans += 1
    for dx,dy in dxy:
        if 0 <= x+dx < h and 0 <= y+dy < w:
            if c[x+dx][y+dy] == (i%2)^1:
                exit(print(ans))
            if (x+dx,y+dy) not in s[i%2]:
                s[i%2].add((x+dx,y+dy))
                heapq.heappush(color[i%2],(grid[x+dx][y+dy],x+dx,y+dy))

0