結果

問題 No.13 囲みたい!
ユーザー taisuketaisuke
提出日時 2023-03-02 15:22:35
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 879 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-09-17 10:23:09
合計ジャッジ時間 1,361 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

w, h = map(int, input().split())
a = [[*map(int, input().split())] for _ in range(h)]

INF = 10**9

visit = [[False for i in range(w)] for j in range(h)]
dist = [[INF for i in range(w)] for j in range(h)]
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]


def dfs(now, pre, num):
    d = dist[now[0]][now[1]]
    for i in range(4):
        y2 = now[0] + dy[i]
        x2 = now[1] + dx[i]
        if y2 < 0 or y2 > h-1 or x2 < 0 or x2 > w-1:
            continue
        if not a[y2][x2] == num or (y2 == pre[0] and x2 == pre[1]):
            continue

        if dist[y2][x2] == INF:
            dist[y2][x2] = d + 1
            dfs([y2, x2], now, num)
        elif d >= 3:
            print("possible")
            exit(0)


for i in range(h):
    for j in range(w):
        if dist[i][j] == INF:
            dist[i][j] = 0
            dfs([i, j], [-1, -1], a[i][j])


print("impossible")
0