結果
問題 |
No.424 立体迷路
|
ユーザー |
![]() |
提出日時 | 2021-04-26 22:21:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 92 ms / 2,000 ms |
コード長 | 1,727 bytes |
コンパイル時間 | 333 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 76,672 KB |
最終ジャッジ日時 | 2024-07-05 08:15:49 |
合計ジャッジ時間 | 2,522 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 21 |
ソースコード
class UnionFind: def __init__(self, n): self.par = [-1]*n self.rank = [0]*n def Find(self, x): if self.par[x] < 0: return x else: self.par[x] = self.Find(self.par[x]) return self.par[x] def Unite(self, x, y): x = self.Find(x) y = self.Find(y) if x != y: if self.rank[x] < self.rank[y]: self.par[y] += self.par[x] self.par[x] = y else: self.par[x] += self.par[y] self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 def Same(self, x, y): return self.Find(x) == self.Find(y) def Size(self, x): return -self.par[self.Find(x)] h, w = map(int, input().split()) sx, sy, gx, gy = map(int, input().split()) sx, sy = sx-1, sy-1 gx, gy = gx-1, gy-1 B = [] for i in range(h): b = list(str(input())) b = [int(i) for i in b] B.append(b) uf = UnionFind(h*w) for x in range(h): for y in range(w): v = x*w+y for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1): nx, ny = x+dx, y+dy if 0 <= nx < h and 0 <= ny < w: if abs(B[x][y]-B[nx][ny]) <= 1: u = nx*w+ny uf.Unite(u, v) for dx, dy in (-2, 0), (2, 0), (0, -2), (0, 2): nx, ny = x+dx, y+dy if 0 <= nx < h and 0 <= ny < w: mx, my = (nx+x)//2, (ny+y)//2 if B[x][y] == B[nx][ny] and B[mx][my] < B[x][y]: u = nx*w+ny uf.Unite(u, v) s = sx*w+sy g = gx*w+gy if uf.Same(s, g): print('YES') else: print('NO')