結果

問題 No.13 囲みたい!
ユーザー cologne
提出日時 2022-01-20 17:59:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 106 ms / 5,000 ms
コード長 874 bytes
コンパイル時間 536 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 79,360 KB
最終ジャッジ日時 2024-11-24 00:44:47
合計ジャッジ時間 2,443 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**5)
input = sys.stdin.readline


def main():
    W, H = map(int, input().split())
    A = [list(map(int, input().split())) for i in range(H)]

    vis = [[False]*W for i in range(H)]

    def dfs(cx, cy, px, py):
        if vis[cx][cy]:
            return True
        vis[cx][cy] = True
        for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
            nx, ny = cx + dx, cy + dy
            if nx == px and ny == py:
                continue
            if 0 <= nx < H and 0 <= ny < W and A[nx][ny] == A[cx][cy]:
                if dfs(nx, ny, cx, cy):
                    return True

        return False

    for i in range(H):
        for j in range(W):
            if not vis[i][j] and dfs(i, j, -1, -1):
                print('possible')
                return

    print('impossible')


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