結果

問題 No.2731 Two Colors
ユーザー 👑 KA37RIKA37RI
提出日時 2024-04-19 22:26:37
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,482 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 82,216 KB
実行使用メモリ 132,396 KB
最終ジャッジ日時 2024-04-19 22:27:09
合計ジャッジ時間 27,856 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 AC 117 ms
77,184 KB
testcase_04 AC 183 ms
78,864 KB
testcase_05 AC 244 ms
79,148 KB
testcase_06 AC 329 ms
86,096 KB
testcase_07 AC 273 ms
85,092 KB
testcase_08 AC 148 ms
77,564 KB
testcase_09 AC 541 ms
105,548 KB
testcase_10 AC 130 ms
77,244 KB
testcase_11 AC 1,080 ms
129,404 KB
testcase_12 AC 532 ms
103,404 KB
testcase_13 AC 998 ms
121,400 KB
testcase_14 AC 352 ms
90,096 KB
testcase_15 AC 178 ms
78,144 KB
testcase_16 AC 572 ms
95,456 KB
testcase_17 AC 545 ms
99,404 KB
testcase_18 AC 210 ms
80,968 KB
testcase_19 AC 583 ms
96,808 KB
testcase_20 AC 436 ms
92,084 KB
testcase_21 AC 223 ms
80,972 KB
testcase_22 AC 795 ms
115,824 KB
testcase_23 AC 307 ms
85,876 KB
testcase_24 AC 234 ms
80,988 KB
testcase_25 AC 110 ms
76,392 KB
testcase_26 AC 583 ms
102,544 KB
testcase_27 AC 668 ms
110,704 KB
testcase_28 AC 640 ms
106,124 KB
testcase_29 AC 309 ms
86,512 KB
testcase_30 AC 392 ms
89,208 KB
testcase_31 AC 366 ms
88,388 KB
testcase_32 AC 1,067 ms
132,396 KB
testcase_33 AC 112 ms
52,992 KB
testcase_34 AC 37 ms
52,224 KB
testcase_35 AC 35 ms
52,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def solve(h, w, a):
  col = [[-1] * w for _ in range(h)]

  ans = 0
  queue0 = [(a[0][0], 0, 0)]
  queue1 = [(a[h-1][w-1], h-1, w-1)]
  while True:
    ans += 1
    filled = False
    while True:
      if queue0:
        _, i, j = heapq.heappop(queue0)
        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:
            heapq.heappush(queue0, (a[ni][nj], ni, nj))
        if filled:
          break

    ans += 1
    while True:
      if queue1:
        _, i, j = heapq.heappop(queue1)
        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:
            heapq.heappush(queue1, (a[ni][nj], ni, nj))
        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()
0