結果

問題 No.13 囲みたい!
ユーザー szkhtsszkhts
提出日時 2022-06-19 08:44:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 867 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-04-19 12:57:10
合計ジャッジ時間 1,291 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,880 KB
testcase_01 AC 27 ms
10,752 KB
testcase_02 AC 29 ms
10,752 KB
testcase_03 RE -
testcase_04 AC 27 ms
11,008 KB
testcase_05 RE -
testcase_06 AC 28 ms
11,136 KB
testcase_07 AC 39 ms
11,008 KB
testcase_08 AC 40 ms
11,264 KB
testcase_09 AC 40 ms
11,136 KB
testcase_10 AC 28 ms
10,880 KB
testcase_11 AC 36 ms
11,136 KB
testcase_12 AC 27 ms
10,880 KB
testcase_13 AC 30 ms
11,008 KB
testcase_14 AC 33 ms
11,008 KB
testcase_15 AC 30 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def ok(y, x):
    return y >= 0 and x >= 0 and y < H and x < W

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def dfs(y, x, pre):
    check[y][x] = True
    for k in range(4):
        if pre == k:
            continue
        ny = y + dy[k]
        nx = x + dx[k]
        if not ok(ny, nx):
            continue
        if field[y][x] != field[ny][nx]:
            continue
        if check[ny][nx]:
            return True
        if dfs(ny, nx, (k + 2) % 4):
            return True

    return False

W, H = map(int, input().split())

field = []
for _ in range(H):
    field.append(list(map(int, input().split())))

check = [[False for _ in range(W)] for _ in range(H)]

for i in range(H):
    for j in range(W):
        if check[i][j]:
            continue
        if dfs(i, j, -1):
            print("possible")
            sys.exit(0)

print("impossible")
0