結果

問題 No.3063 Circle Balancing
ユーザー lam6er
提出日時 2025-04-16 15:22:29
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,133 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 64,640 KB
最終ジャッジ日時 2025-04-16 15:23:39
合計ジャッジ時間 3,188 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 2
other RE * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

R, C = map(int, sys.stdin.readline().split())
sy, sx = map(int, sys.stdin.readline().split())
gy, gx = map(int, sys.stdin.readline().split())

start_y = sy - 1
start_x = sx - 1
goal_y = gy - 1
goal_x = gx - 1

grid = []
for _ in range(R):
    grid.append(sys.stdin.readline().strip())

distance = [[-1 for _ in range(C)] for _ in range(R)]
queue = deque()

distance[start_y][start_x] = 0

if start_y == goal_y and start_x == goal_x:
    print(0)
    sys.exit()

queue.append((start_y, start_x))

directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
found = False

while queue:
    y, x = queue.popleft()
    current_dist = distance[y][x]
    
    for dy, dx in directions:
        ny = y + dy
        nx = x + dx
        
        if 0 <= ny < R and 0 <= nx < C:
            if grid[ny][nx] == '.' and distance[ny][nx] == -1:
                distance[ny][nx] = current_dist + 1
                if ny == goal_y and nx == goal_x:
                    print(distance[ny][nx])
                    found = True
                    break
                queue.append((ny, nx))
    if found:
        break
0