結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-08-04 13:48:30 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 32 ms / 2,000 ms |
| コード長 | 1,385 bytes |
| コンパイル時間 | 249 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 11,008 KB |
| 最終ジャッジ日時 | 2024-07-08 02:42:01 |
| 合計ジャッジ時間 | 1,580 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
from collections import deque
def bfs(sx, sy):
queue = deque([])
queue.append((sx, sy))
visited = [[-1 for _ in range(W)] for _ in range(H)]
visited[sx][sy] = 1
while len(queue) > 0:
current_x, current_y = queue.popleft()
for next_x, next_y in [(current_x-1, current_y), (current_x+1, current_y), (current_x, current_y-1), (current_x, current_y+1)]:
if 0 <= next_x < H and 0 <= next_y < W:
if C[next_x][next_y] == '.' and visited[next_x][next_y] == -1:
visited[next_x][next_y] = 1
queue.append((next_x, next_y))
return visited
W, H = map(int, input().split())
C = [input() for _ in range(H)]
for i in range(H):
for j in range(W):
if C[i][j] == '.':
amx, amy = i, j
break
visitedA = bfs(amx, amy)
holeA = []
for i in range(H):
for j in range(W):
if visitedA[i][j] == 1:
holeA.append((i, j))
for i in range(H):
for j in range(W):
if C[i][j] == '.' and visitedA[i][j] == -1:
bmx, bmy = i, j
break
visitedB = bfs(bmx, bmy)
holeB = []
for i in range(H):
for j in range(W):
if visitedB[i][j] == 1:
holeB.append((i, j))
ans = 10 ** 18
for Ax, Ay in holeA:
for Bx, By in holeB:
ans = min(ans, abs(Ax-Bx) + abs(Ay-By) - 1)
print(ans)