結果

問題 No.13 囲みたい!
ユーザー szkhtsszkhts
提出日時 2022-06-19 09:40:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 52 ms / 5,000 ms
コード長 907 bytes
コンパイル時間 68 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2024-04-19 13:55:56
合計ジャッジ時間 1,218 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,880 KB
testcase_01 AC 25 ms
10,752 KB
testcase_02 AC 28 ms
10,880 KB
testcase_03 AC 49 ms
13,312 KB
testcase_04 AC 52 ms
15,360 KB
testcase_05 AC 45 ms
12,800 KB
testcase_06 AC 42 ms
11,136 KB
testcase_07 AC 43 ms
11,136 KB
testcase_08 AC 44 ms
11,264 KB
testcase_09 AC 42 ms
11,264 KB
testcase_10 AC 28 ms
11,008 KB
testcase_11 AC 39 ms
11,136 KB
testcase_12 AC 26 ms
10,880 KB
testcase_13 AC 31 ms
11,008 KB
testcase_14 AC 31 ms
11,136 KB
testcase_15 AC 27 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(12000)

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

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

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

def dfs(x, y):
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    visit[x][y] = True
    c = (1, 0)

    for k in range(4):
        ny = y + dy[k]
        nx = x + dx[k]
        if ok(nx, ny) and field[nx][ny] == field[x][y]:
            if not visit[nx][ny]:
                res = dfs(nx, ny)
                c = (c[0] + res[0], c[1] + res[1])
            c = (c[0], c[1] + 1)

    return c

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

for i in range(H):
    for j in range(W):
        if not visit[i][j]:
            res = dfs(i, j)
            if res[1] // 2 != res[0] - 1:
                ans = True

if ans:
    print("possible")
else:
    print("impossible")
0