結果
問題 | No.157 2つの空洞 |
ユーザー | yansi819 |
提出日時 | 2024-05-25 09:21:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 55 ms / 2,000 ms |
コード長 | 1,968 bytes |
コンパイル時間 | 404 ms |
コンパイル使用メモリ | 82,260 KB |
実行使用メモリ | 63,232 KB |
最終ジャッジ日時 | 2024-12-20 19:34:49 |
合計ジャッジ時間 | 2,093 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
52,480 KB |
testcase_01 | AC | 42 ms
52,864 KB |
testcase_02 | AC | 42 ms
52,736 KB |
testcase_03 | AC | 42 ms
52,608 KB |
testcase_04 | AC | 42 ms
52,608 KB |
testcase_05 | AC | 46 ms
52,480 KB |
testcase_06 | AC | 42 ms
52,608 KB |
testcase_07 | AC | 42 ms
52,224 KB |
testcase_08 | AC | 41 ms
52,864 KB |
testcase_09 | AC | 42 ms
52,480 KB |
testcase_10 | AC | 40 ms
52,480 KB |
testcase_11 | AC | 40 ms
52,480 KB |
testcase_12 | AC | 45 ms
52,992 KB |
testcase_13 | AC | 42 ms
52,864 KB |
testcase_14 | AC | 48 ms
58,624 KB |
testcase_15 | AC | 49 ms
59,520 KB |
testcase_16 | AC | 46 ms
53,376 KB |
testcase_17 | AC | 55 ms
63,232 KB |
testcase_18 | AC | 44 ms
53,376 KB |
testcase_19 | AC | 42 ms
52,480 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)