結果

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

ソースコード

diff #

import sys
from collections import deque

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

# Convert to 0-based indices
sy -= 1
sx -= 1
gy -= 1
gx -= 1

# Read maze
maze = []
for _ in range(R):
    row = sys.stdin.readline().strip()
    maze.append(row)

# Initialize distance array and queue
dist = [[-1] * C for _ in range(R)]
q = deque()
dist[sy][sx] = 0
q.append((sy, sx))

# Directions: up, down, left, right
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

# BFS loop
while q:
    y, x = q.popleft()
    current_dist = dist[y][x]
    
    # Check if current position is the goal
    if y == gy and x == gx:
        print(current_dist)
        sys.exit()
    
    # Explore all four directions
    for i in range(4):
        ny = y + dy[i]
        nx = x + dx[i]
        
        # Check if the next cell is within bounds, not a wall, and not visited
        if 0 <= ny < R and 0 <= nx < C:
            if maze[ny][nx] == '.' and dist[ny][nx] == -1:
                dist[ny][nx] = current_dist + 1
                q.append((ny, nx))

# According to the problem statement, the goal is always reachable, so this line is theoretically unreachable
print(-1)
0