結果

問題 No.13 囲みたい!
ユーザー tnodino
提出日時 2022-02-02 22:19:50
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 818 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-06-11 09:36:49
合計ジャッジ時間 1,324 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
setrecursionlimit(10**6)

def dfs(x, y, rx, ry):
    flg[x][y] = False
    Check[x][y] = False
    for i in range(4):
        nx,my = x+dx[i],y+dy[i]
        if nx < 0 or H <= nx or my < 0 or W <= my:
            continue
        if nx == rx and my == ry:
            continue
        if M[x][y] == M[nx][my]:
            if flg[nx][my]:
                dfs(nx, my, x, y)
            else:
                print('possible')
                exit()
    flg[x][y] = True

W,H = map(int,input().split())
M = [list(map(int,input().split())) for _ in range(H)]
flg = [[True] * W for _ in range(H)]
Check = [[True] * W for _ in range(H)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for i in range(H):
    for j in range(W):
        if Check[i][j]:
            dfs(i, j, -1, -1)
print('impossible')
0