結果

問題 No.13 囲みたい!
コンテスト
ユーザー sotanishy
提出日時 2021-03-16 22:02:57
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 787 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 363 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 76,160 KB
最終ジャッジ日時 2026-05-08 19:28:25
合計ジャッジ時間 2,148 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

W, H = map(int, input().split())
M = [list(map(int, input().split())) for _ in range(H)]
used = [[False] * W for _ in range(H)]
for si in range(H):
    for sj in range(W):
        if used[si][sj]:
            continue
        st = [(si, sj, -1, -1)]
        used[si][sj] = True
        while st:
            i, j, pi, pj = st.pop()
            for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                ni, nj = i + di, j + dj
                if 0 <= ni < H and 0 <= nj < W and M[ni][nj] == M[i][j] and (ni, nj) != (pi, pj):
                    if used[ni][nj]:
                        print('possible')
                        exit()
                    st.append((ni, nj, i, j))
                    used[ni][nj] = True
print('impossible')
0