結果

問題 No.13 囲みたい!
ユーザー nagitaosunagitaosu
提出日時 2020-03-13 13:11:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 91 ms / 5,000 ms
コード長 1,033 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 76,416 KB
最終ジャッジ日時 2024-05-01 17:52:08
合計ジャッジ時間 1,836 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,096 KB
testcase_01 AC 39 ms
51,840 KB
testcase_02 AC 40 ms
51,840 KB
testcase_03 AC 55 ms
64,768 KB
testcase_04 AC 42 ms
59,264 KB
testcase_05 AC 72 ms
73,216 KB
testcase_06 AC 56 ms
63,360 KB
testcase_07 AC 91 ms
76,416 KB
testcase_08 AC 61 ms
72,448 KB
testcase_09 AC 61 ms
72,704 KB
testcase_10 AC 54 ms
63,488 KB
testcase_11 AC 58 ms
69,760 KB
testcase_12 AC 45 ms
59,264 KB
testcase_13 AC 54 ms
65,152 KB
testcase_14 AC 54 ms
65,152 KB
testcase_15 AC 39 ms
52,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
input = sys.stdin.readline

w, h = map(int, input().split())
field = []
visited = [[0] * (w+2) for _ in range(h+2)]
field.append([-1] * (w + 2))
for _ in range(h):
    line = [-1] + [int(item) for item in input().split()] + [-1]
    field.append(line)
field.append([-1] * (w + 2))


delta = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def dfs(sx, sy):
    st = [(sx, sy, -1, -1)]
    c = field[sx][sy]
    visited[sx][sy] = 1
    while st:
        x, y, px, py = st.pop()
        for dx, dy in delta:
            if x + dx == px and y + dy == py:
                continue
            if field[x + dx][y + dy] == c:
                if not visited[x + dx][y + dy]:
                    visited[x + dx][y + dy] = 1
                    st.append((x+dx, y+dy, x, y))
                elif visited[x + dx][y + dy]:
                    print("possible")
                    exit()
for i in range(1, 1+h):
    for j in range(1, 1+w):
        if visited[i][j]:
            continue
        dfs(i, j)
print("impossible")
0