結果
問題 | No.13 囲みたい! |
ユーザー |
![]() |
提出日時 | 2016-05-27 09:14:42 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 54 ms / 5,000 ms |
コード長 | 1,867 bytes |
コンパイル時間 | 505 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 12,672 KB |
最終ジャッジ日時 | 2024-10-13 10:53:32 |
合計ジャッジ時間 | 1,470 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
#!/usr/bin/env python3 import array import collections import itertools REC_LIMIT = 10000 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 = collections.defaultdict(bool) self.pred = collections.defaultdict(lambda: UNDEF) def in_field(self, r, c): return 0 <= r < self.height and 0 <= c < self.width def in_cycle(self, r0, c0, num): self.visited[(r0, c0)] = True q = collections.deque() q.append((r0, c0)) while q: (r1, c1) = q.pop() for dr, dc in DELTAS: (r, c) = (r1 + dr, c1 + dc) if not self.in_field(r, c): continue elif self.field[r][c] != num: continue if self.visited[(r, c)]: if self.pred[(r1, c1)] != (r, c): return True else: continue self.visited[(r, c)] = True self.pred[(r, c)] = (r1, c1) q.append((r, c)) return False def judge(self): rcs = itertools.product(range(self.height), range(self.width)) for r, c in rcs: if not self.visited[(r, c)]: answer = self.in_cycle(r, c, self.field[r][c]) if answer: return True else: return False def main(): 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()