結果

問題 No.13 囲みたい!
ユーザー kohei2019kohei2019
提出日時 2021-02-09 22:28:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 129 ms / 5,000 ms
コード長 1,068 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 78,776 KB
最終ジャッジ日時 2023-09-21 10:18:31
合計ジャッジ時間 2,841 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
71,792 KB
testcase_01 AC 87 ms
71,608 KB
testcase_02 AC 86 ms
71,696 KB
testcase_03 AC 111 ms
77,652 KB
testcase_04 AC 95 ms
76,916 KB
testcase_05 AC 117 ms
78,040 KB
testcase_06 AC 107 ms
77,940 KB
testcase_07 AC 129 ms
78,776 KB
testcase_08 AC 104 ms
78,116 KB
testcase_09 AC 117 ms
77,956 KB
testcase_10 AC 101 ms
78,108 KB
testcase_11 AC 105 ms
77,972 KB
testcase_12 AC 98 ms
77,516 KB
testcase_13 AC 105 ms
78,284 KB
testcase_14 AC 102 ms
77,736 KB
testcase_15 AC 92 ms
71,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
import sys
W,H = map(int,input().split())
lsM = [list(map(int,input().split())) for i in range(H)]
usedn = [[False]*(W) for i in range(H)]
before = [[(-1,-1) for j in range(W)] for i in range(H)]
dxy = [(1,0),(0,1),(-1,0),(0,-1)]
#DFSで探す閉路判定
for i in range(H):
    for j in range(W):
        if usedn[i][j]:
            continue
        d = collections.deque([(i,j)])
        n = lsM[i][j]
        while d:
            x,y = d.pop()
            if usedn[x][y]:
                continue
            usedn[x][y] = True
            for dx,dy in dxy:
                if 0 <= x+dx < H and 0 <= y+dy < W:
                    if lsM[x+dx][y+dy] != n:
                        continue
                    if usedn[x+dx][y+dy]:
                        if before[x][y] == (x+dx,y+dy):
                            continue
                        else:
                            print('possible')
                            sys.exit()
                    before[x+dx][y+dy] = (x,y)
                    d.append((x+dx,y+dy))
print('impossible')
0