結果
問題 | No.157 2つの空洞 |
ユーザー | yansi819 |
提出日時 | 2024-05-25 09:21:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 52 ms / 2,000 ms |
コード長 | 1,968 bytes |
コンパイル時間 | 281 ms |
コンパイル使用メモリ | 82,396 KB |
実行使用メモリ | 63,804 KB |
最終ジャッジ日時 | 2024-05-25 09:21:57 |
合計ジャッジ時間 | 2,440 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
53,648 KB |
testcase_01 | AC | 38 ms
53,292 KB |
testcase_02 | AC | 39 ms
54,376 KB |
testcase_03 | AC | 38 ms
53,012 KB |
testcase_04 | AC | 38 ms
53,488 KB |
testcase_05 | AC | 39 ms
54,684 KB |
testcase_06 | AC | 39 ms
52,652 KB |
testcase_07 | AC | 38 ms
54,468 KB |
testcase_08 | AC | 39 ms
53,944 KB |
testcase_09 | AC | 39 ms
54,244 KB |
testcase_10 | AC | 39 ms
53,464 KB |
testcase_11 | AC | 39 ms
53,200 KB |
testcase_12 | AC | 39 ms
54,416 KB |
testcase_13 | AC | 39 ms
52,748 KB |
testcase_14 | AC | 44 ms
58,960 KB |
testcase_15 | AC | 46 ms
60,072 KB |
testcase_16 | AC | 41 ms
53,908 KB |
testcase_17 | AC | 52 ms
63,804 KB |
testcase_18 | AC | 41 ms
53,856 KB |
testcase_19 | AC | 39 ms
53,512 KB |
ソースコード
class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self._n if self.parent_or_size[a] < 0: return a self.parent_or_size[a] = self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self, a): assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [self.leader(i) for i in range(self._n)] result = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return [r for r in result if r != []] dx = [-1, 0, 0, 1] dy = [0, -1, 1, 0] W, H = map(int, input().split()) C = [list(input().rstrip()) for _ in range(H)] uf = DSU(W * H) for i in range(H): for j in range(W): if C[i][j] == ".": id = i * W + j for k in range(4): ni = i + dx[k] nj = j + dy[k] if 0 > ni or ni >= H or 0 > nj or nj >= W or C[ni][nj] == "#": continue nid = ni * W + nj uf.merge(id, nid) MIN = W + H g = [] for gr in uf.groups(): x, y = gr[0] // W, gr[0] % W if C[x][y] == ".": g.append(gr) for i in g[0]: for j in g[1]: x, y = i // W, i % W nx, ny = j // W, j % W #print(x, y, nx, ny) MIN = min(MIN, abs(x - nx) + abs(y - ny) - 1) print(MIN)