結果

問題 No.2731 Two Colors
ユーザー budoubudou
提出日時 2024-04-20 08:21:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,114 ms / 3,000 ms
コード長 1,162 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 82,288 KB
実行使用メモリ 113,012 KB
最終ジャッジ日時 2024-04-20 08:21:50
合計ジャッジ時間 15,184 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,110 ms
102,900 KB
testcase_01 AC 1,042 ms
102,984 KB
testcase_02 AC 1,114 ms
102,496 KB
testcase_03 AC 118 ms
79,332 KB
testcase_04 AC 173 ms
80,544 KB
testcase_05 AC 147 ms
79,696 KB
testcase_06 AC 209 ms
84,780 KB
testcase_07 AC 181 ms
83,852 KB
testcase_08 AC 138 ms
79,524 KB
testcase_09 AC 323 ms
97,916 KB
testcase_10 AC 127 ms
79,704 KB
testcase_11 AC 524 ms
110,192 KB
testcase_12 AC 325 ms
98,640 KB
testcase_13 AC 470 ms
105,792 KB
testcase_14 AC 236 ms
87,376 KB
testcase_15 AC 149 ms
79,940 KB
testcase_16 AC 286 ms
90,300 KB
testcase_17 AC 288 ms
92,900 KB
testcase_18 AC 183 ms
81,644 KB
testcase_19 AC 310 ms
91,656 KB
testcase_20 AC 253 ms
89,372 KB
testcase_21 AC 187 ms
81,548 KB
testcase_22 AC 349 ms
103,520 KB
testcase_23 AC 204 ms
84,228 KB
testcase_24 AC 188 ms
81,396 KB
testcase_25 AC 119 ms
79,576 KB
testcase_26 AC 347 ms
96,652 KB
testcase_27 AC 374 ms
101,312 KB
testcase_28 AC 325 ms
95,768 KB
testcase_29 AC 206 ms
83,652 KB
testcase_30 AC 223 ms
86,900 KB
testcase_31 AC 228 ms
85,664 KB
testcase_32 AC 491 ms
113,012 KB
testcase_33 AC 56 ms
68,400 KB
testcase_34 AC 56 ms
68,112 KB
testcase_35 AC 55 ms
67,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing
import sys
from collections import defaultdict
import heapq
input = lambda: sys.stdin.readline().strip()
inf = 10**18
mod = 998244353
# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
# sys.setrecursionlimit(10**6)


def solve():
  H, W = map(int, input().split())
  def enc(h, w):
    return h*W + w
  def dec(x):
    return x//W, x%W
  grid = []
  for _ in range(H):
    grid.append(list(map(int, input().split())))
  color = [[-1]*W for _ in range(H)]
  q = [[(grid[-1][-1], enc(H-1, W-1))], [(grid[0][0], enc(0, 0))]]
  cnt = -2
  while True:
    while True:
      _, x = heapq.heappop(q[(cnt-1)%2])
      h, w = dec(x)
      if color[h][w] == -1:
        break
    cnt += 1
    color[h][w] = cnt%2
    # 隣あっているマスに異なるいろがあったら終わり
    for dh, dw in zip([1, 0, -1, 0], [0, 1, 0, -1]):
      if 0 <= dh + h <= H-1 and 0 <= dw + w <= W-1:
        if color[dh + h][dw + w]==-1:
          heapq.heappush(q[cnt%2], (grid[dh+h][dw+w], enc(dh+h, dw+w)))
        if color[dh + h][dw + w]==(cnt-1)%2:
          print(cnt)
          return

def main():
  t = 1
  for _ in range(t):
    solve()
main()
0