結果

問題 No.2731 Two Colors
ユーザー mkawa2mkawa2
提出日時 2024-04-19 22:05:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 958 ms / 3,000 ms
コード長 1,653 bytes
コンパイル時間 521 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 271,260 KB
最終ジャッジ日時 2024-04-19 22:06:16
合計ジャッジ時間 12,494 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 958 ms
270,740 KB
testcase_01 AC 952 ms
271,260 KB
testcase_02 AC 934 ms
271,020 KB
testcase_03 AC 84 ms
76,800 KB
testcase_04 AC 111 ms
84,096 KB
testcase_05 AC 124 ms
79,872 KB
testcase_06 AC 166 ms
99,328 KB
testcase_07 AC 155 ms
106,496 KB
testcase_08 AC 91 ms
78,848 KB
testcase_09 AC 382 ms
169,856 KB
testcase_10 AC 88 ms
77,184 KB
testcase_11 AC 494 ms
158,892 KB
testcase_12 AC 365 ms
169,472 KB
testcase_13 AC 410 ms
157,184 KB
testcase_14 AC 226 ms
113,064 KB
testcase_15 AC 93 ms
78,152 KB
testcase_16 AC 228 ms
115,200 KB
testcase_17 AC 263 ms
137,344 KB
testcase_18 AC 115 ms
83,840 KB
testcase_19 AC 243 ms
115,584 KB
testcase_20 AC 247 ms
157,232 KB
testcase_21 AC 122 ms
85,248 KB
testcase_22 AC 446 ms
190,080 KB
testcase_23 AC 166 ms
99,712 KB
testcase_24 AC 125 ms
86,400 KB
testcase_25 AC 94 ms
76,544 KB
testcase_26 AC 323 ms
177,792 KB
testcase_27 AC 397 ms
190,592 KB
testcase_28 AC 305 ms
137,856 KB
testcase_29 AC 153 ms
94,976 KB
testcase_30 AC 202 ms
115,968 KB
testcase_31 AC 179 ms
96,896 KB
testcase_32 AC 537 ms
191,360 KB
testcase_33 AC 35 ms
52,608 KB
testcase_34 AC 36 ms
52,864 KB
testcase_35 AC 35 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353

from heapq import *

h,w=LI()
aa=LLI(h)
tt=[[-1]*w for _ in range(h)]

a2ij={}
for i in range(h):
    for j,a in enumerate(aa[i]):
        a2ij[a]=(i,j)

hp=[[],[]]
st=[set(),set()]
tt[0][0]=1
tt[-1][-1]=0
st[1].add(aa[0][0])
st[0].add(aa[-1][-1])

k=1
for i,j in [(0,0),(h-1,w-1)]:
    for di, dj in dij:
        ni, nj = i+di, j+dj
        if ni < 0 or nj < 0 or ni >= h or nj >= w: continue
        st[k].add(aa[ni][nj])
        heappush(hp[k],aa[ni][nj])
    k^=1
# print(hp,st)

ans=0
k=1
while 1:
    a=heappop(hp[k])
    i,j=a2ij[a]
    tt[i][j]=k
    ans+=1
    for di, dj in dij:
        ni, nj = i+di, j+dj
        if ni < 0 or nj < 0 or ni >= h or nj >= w: continue
        if tt[ni][nj]==k^1:
            print(ans)
            exit()
        if aa[ni][nj] in st[k]:continue
        st[k].add(aa[ni][nj])
        heappush(hp[k],aa[ni][nj])
    k^=1
    # print(k,hp,st)




0