World Robot Contest2025-2026Algorithm Application ThemeJunior Highwrc.hao.work
WRC
Contest Archive / Structured Dossiers青少年算法应用训练档案馆

把训练题、知识点、执行证据和最终解题档案统一归档成可直接浏览的竞赛资料库。

Archive30 Cases

四大文化赛道完整展开

AccessHTTPS

完整题面 / 题解 / 运行证据

No Rounded CornersTailwind FirstDossier Ready
06-deliverables/appendix-code.md

民族展馆导览:最短参观路径搜索 代码附录

站内文件视图直接读取仓库内容,Markdown 使用文档排版渲染,其余文本文件保持原始排版,方便校对训练证据链。

文件类型Markdown

10-cases/s4-jh-07-hall-navigation/06-deliverables/appendix-code.md

Python 主实现

源文件: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")

C++ 对照实现

源文件:main.cpp

  • 实现状态:当前已有可执行实现
#include <algorithm>
#include <deque>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int rows, cols;
    if (!(cin >> rows >> cols)) {
        return 0;
    }
    vector<string> grid(rows);
    pair<int, int> start = {-1, -1};
    pair<int, int> target = {-1, -1};
    for (int row = 0; row < rows; ++row) {
        cin >> grid[row];
        for (int col = 0; col < cols; ++col) {
            if (grid[row][col] == 'S') {
                start = {row, col};
            } else if (grid[row][col] == 'T') {
                target = {row, col};
            }
        }
    }
    const int dr[4] = {1, 0, 0, -1};
    const int dc[4] = {0, -1, 1, 0};
    const char mark[4] = {'D', 'L', 'R', 'U'};
    deque<pair<int, int>> queue;
    vector<vector<int>> visited(rows, vector<int>(cols, 0));
    vector<vector<tuple<int, int, char>>> parent(rows, vector<tuple<int, int, char>>(cols, {-1, -1, '?'}));
    queue.push_back(start);
    visited[start.first][start.second] = 1;
    while (!queue.empty()) {
        auto [row, col] = queue.front();
        queue.pop_front();
        if (make_pair(row, col) == target) {
            break;
        }
        for (int dir = 0; dir < 4; ++dir) {
            int nxt_row = row + dr[dir];
            int nxt_col = col + dc[dir];
            if (nxt_row < 0 || nxt_row >= rows || nxt_col < 0 || nxt_col >= cols) {
                continue;
            }
            if (visited[nxt_row][nxt_col] || grid[nxt_row][nxt_col] == '#') {
                continue;
            }
            visited[nxt_row][nxt_col] = 1;
            parent[nxt_row][nxt_col] = {row, col, mark[dir]};
            queue.push_back({nxt_row, nxt_col});
        }
    }
    if (!visited[target.first][target.second]) {
        cout << "distance=-1\n";
        cout << "path=NONE\n";
        return 0;
    }
    string path;
    pair<int, int> cur = target;
    while (cur != start) {
        auto [prev_row, prev_col, step] = parent[cur.first][cur.second];
        path.push_back(step);
        cur = {prev_row, prev_col};
    }
    reverse(path.begin(), path.end());
    cout << "distance=" << path.size() << "\n";
    cout << "path=" << path << "\n";
    return 0;
}