結果

問題 No.13 囲みたい!
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-05-26 22:55:39
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,733 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 87,040 KB
最終ジャッジ日時 2024-04-16 17:41:42
合計ジャッジ時間 2,402 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,992 KB
testcase_01 AC 42 ms
52,864 KB
testcase_02 AC 44 ms
52,352 KB
testcase_03 AC 133 ms
81,536 KB
testcase_04 RE -
testcase_05 AC 143 ms
82,688 KB
testcase_06 AC 78 ms
71,296 KB
testcase_07 AC 109 ms
77,568 KB
testcase_08 AC 97 ms
77,184 KB
testcase_09 AC 98 ms
77,184 KB
testcase_10 AC 87 ms
74,240 KB
testcase_11 AC 96 ms
77,056 KB
testcase_12 AC 68 ms
65,792 KB
testcase_13 AC 93 ms
76,800 KB
testcase_14 AC 96 ms
77,056 KB
testcase_15 AC 45 ms
53,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import array
import itertools
import sys


REC_LIMIT = 3600
DELTAS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
UNDEF = (-1, -1)


class Solver(object):

    def __init__(self, width, height, field):
        self.width = width
        self.height = height
        self.field = field
        self.visited = [array.array("B", (False for _ in range(width)))
                        for _ in range(height)]
        self.pred = [[UNDEF for _ in range(width)] for _ in range(height)]
        self.answer = False

    def in_field(self, r, c):
        return 0 <= r < self.height and 0 <= c < self.width

    def dfs(self, r0, c0, num):
        if self.field[r0][c0] != num:
            return
        elif self.visited[r0][c0]:
            self.answer = True
            return
        self.visited[r0][c0] = True
        for dr, dc in DELTAS:
            (r, c) = (r0 + dr, c0 + dc)
            if not self.in_field(r, c):
                continue
            elif self.pred[r0][c0] == (r, c):
                continue
            self.pred[r][c] = (r0, c0)
            self.dfs(r, c, num)

    def judge(self):
        rcs = itertools.product(range(self.height), range(self.width))
        for r, c in rcs:
            if not self.visited[r][c]:
                self.dfs(r, c, self.field[r][c])
            if self.answer:
                return True
        else:
            return False


def main():
    sys.setrecursionlimit(REC_LIMIT)
    width, height = map(int, input().split())
    field = [array.array("I", map(int, input().split()))
             for _ in range(height)]
    solver = Solver(width, height, field)
    print("possible" if solver.judge() else "impossible")


if __name__ == '__main__':
    main()
0