四大文化赛道完整展开
03-execution/run-001/source-snapshot/main.py
main.py
站内文件视图直接读取仓库内容,Markdown 使用文档排版渲染,其余文本文件保持原始排版,方便校对训练证据链。
文件类型.py
10-cases/s4-jh-07-hall-navigation/03-execution/run-001/source-snapshot/main.py
from collections import deque
import sys
def solve(data: str) -> str:
raw_lines = [line.rstrip() for line in data.splitlines() if line.strip()]
if not raw_lines:
return ""
rows, cols = map(int, raw_lines[0].split())
grid = raw_lines[1:1 + rows]
start = (-1, -1)
target = (-1, -1)
for row in range(rows):
for col in range(cols):
if grid[row][col] == "S":
start = (row, col)
elif grid[row][col] == "T":
target = (row, col)
directions = ((1, 0, "D"), (0, -1, "L"), (0, 1, "R"), (-1, 0, "U"))
queue = deque([start])
visited = [[False] * cols for _ in range(rows)]
parent = [[None] * cols for _ in range(rows)]
visited[start[0]][start[1]] = True
while queue:
row, col = queue.popleft()
if (row, col) == target:
break
for dr, dc, mark in directions:
nxt_row = row + dr
nxt_col = col + dc
if not (0 <= nxt_row < rows and 0 <= nxt_col < cols):
continue
if visited[nxt_row][nxt_col] or grid[nxt_row][nxt_col] == "#":
continue
visited[nxt_row][nxt_col] = True
parent[nxt_row][nxt_col] = (row, col, mark)
queue.append((nxt_row, nxt_col))
if not visited[target[0]][target[1]]:
return "distance=-1\npath=NONE"
path = []
cur = target
while cur != start:
row, col = cur
prev_row, prev_col, mark = parent[row][col]
path.append(mark)
cur = (prev_row, prev_col)
path.reverse()
return f"distance={len(path)}\npath={''.join(path)}"
if __name__ == "__main__":
sys.stdout.write(solve(sys.stdin.read()).strip())
sys.stdout.write("\n")