結果

問題 No.13 囲みたい!
ユーザー 👑 colognecologne
提出日時 2022-01-20 17:59:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 104 ms / 5,000 ms
コード長 874 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 86,864 KB
実行使用メモリ 83,528 KB
最終ジャッジ日時 2023-08-15 16:45:53
合計ジャッジ時間 2,451 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,188 KB
testcase_01 AC 63 ms
71,120 KB
testcase_02 AC 63 ms
71,116 KB
testcase_03 AC 82 ms
79,724 KB
testcase_04 AC 65 ms
75,744 KB
testcase_05 AC 104 ms
82,416 KB
testcase_06 AC 73 ms
76,420 KB
testcase_07 AC 94 ms
83,528 KB
testcase_08 AC 79 ms
76,904 KB
testcase_09 AC 78 ms
77,068 KB
testcase_10 AC 74 ms
76,252 KB
testcase_11 AC 79 ms
76,416 KB
testcase_12 AC 69 ms
75,328 KB
testcase_13 AC 74 ms
76,236 KB
testcase_14 AC 75 ms
76,448 KB
testcase_15 AC 64 ms
71,264 KB
権限があれば一括ダウンロードができます

ソースコード

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