結果

問題 No.13 囲みたい!
ユーザー Mao-betaMao-beta
提出日時 2024-02-29 00:50:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 2,020 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 77,248 KB
最終ジャッジ日時 2024-02-29 00:50:30
合計ジャッジ時間 2,684 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
55,616 KB
testcase_01 AC 50 ms
55,616 KB
testcase_02 AC 54 ms
57,700 KB
testcase_03 AC 75 ms
72,648 KB
testcase_04 AC 55 ms
63,788 KB
testcase_05 AC 125 ms
76,488 KB
testcase_06 AC 69 ms
70,576 KB
testcase_07 AC 104 ms
77,248 KB
testcase_08 AC 83 ms
76,468 KB
testcase_09 AC 83 ms
76,468 KB
testcase_10 AC 71 ms
72,764 KB
testcase_11 AC 81 ms
76,348 KB
testcase_12 AC 62 ms
65,872 KB
testcase_13 AC 78 ms
76,352 KB
testcase_14 AC 80 ms
76,348 KB
testcase_15 AC 52 ms
57,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353

input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]


def main():
    W, H = NMI()
    M = EI(H)

    steps = [[-1] * W for _ in range(H)]

    for sh in range(H):
        for sw in range(W):
            if steps[sh][sw] >= 0:
                continue


            def bfs(sh, sw):
                x = M[sh][sw]

                queue = deque()
                queue.append([sh, sw, -1, -1])

                steps[sh][sw] = 0

                DH = [0, 0, 1, -1]
                DW = [1, -1, 0, 0]

                while queue:
                    now_h, now_w, prev_h, prev_w = queue.popleft()
                    now_step = steps[now_h][now_w]

                    for dh, dw in zip(DH, DW):
                        goto_h = now_h + dh
                        goto_w = now_w + dw
                        if goto_h == prev_h and goto_w == prev_w:
                            continue

                        goto_step = now_step + 1

                        if goto_h < 0 or goto_h >= H or goto_w < 0 or goto_w >= W:
                            continue
                        if M[goto_h][goto_w] != x:
                            continue
                        if 0 <= steps[goto_h][goto_w] <= goto_step:
                            print("possible")
                            exit()

                        queue.append([goto_h, goto_w, now_h, now_w])
                        steps[goto_h][goto_w] = goto_step

            bfs(sh, sw)

    print("impossible")


if __name__ == "__main__":
    main()
0