跳到正文

题解

题解:UVA12731 太空站之谜 Mysterious Space Station

EnigmaExplorer约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年4月18日
收录
2026年1月31日
更新
2026年9月28日
篇幅
约 9 分钟
题目
UVA12731

版本

  1. v1当前

import java.util.*;

public class Main {

C++
private static final char EMPTY = '.';
private static final char OBSTACLE = '*';
private static final char START = 'S';

private static char[][] grid;
private static boolean[][] visited;
private static int n, m, k;
private static List<Integer> teleportPairs;
private static int[][] teleport;
private static Queue<Coordinate> queue;
private static Coordinate robotPosition;
private static char robotDirection;

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    // Read the initial grid
    while (true) {
        n = in.nextInt();
        m = in.nextInt();
        k = in.nextInt();
        if (n == 0 && m == 0 && k == 0) break;

        grid = new char[n][m];
        visited = new boolean[n][m];
        teleport = new int[n][m];
        teleportPairs = new ArrayList<>();
        queue = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            String line = in.next().trim();
            for (int j = 0; j < m; j++) {
                grid[i][j] = line.charAt(j);
                visited[i][j] = false;
                teleport[i][j] = -1;
            }
        }

        // Find the starting position
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == START) {
                    robotPosition = new Coordinate(i, j);
                    break;
                }
            }
        }

        // Initialize the robot direction
        robotDirection = 'N'; // initial direction can be any direction

        // Start exploring
        explore(out);
    }

    out.flush();
    in.close();
}

private static void explore(PrintWriter out) {
    // Mark the current position as visited
    visited[robotPosition.x][robotPosition.y] = true;

    // Queue for BFS
    queue.add(robotPosition);

    while (!queue.isEmpty()) {
        Coordinate current = queue.poll();

        // Try to move in all four directions
        for (char dir : new char[] {'N', 'S', 'E', 'W'}) {
            Coordinate nextPos = getNextPosition(current, dir);

            if (isWithinGrid(nextPos) && grid[nextPos.x][nextPos.y] != OBSTACLE && !visited[nextPos.x][nextPos.y]) {
                // Move the robot
                out.println("MoveRobot " + dir);
                out.flush();

                String response = readResponse();

                if (response.equals("1")) {
                    robotPosition = nextPos;
                    visited[robotPosition.x][robotPosition.y] = true;
                    queue.add(robotPosition);
                } else {
                    // Mark the cell as visited even if the move was unsuccessful
                    visited[robotPosition.x][robotPosition.y] = true;
                }
            }
        }
    }

    // After exploring, answer the teleport controllers
    for (int i = 1; i <= k; i++) {
        int[] pair = new int[2];
        pair[0] = teleportPairs.get(2 * i - 2);
        pair[1] = teleportPairs.get(2 * i - 1);
        out.println("Answer " + pair[0] + " " + pair[1]);
        out.flush();
    }
}

private static Coordinate getNextPosition(Coordinate current, char dir) {
    switch (dir) {
        case 'N':
            return new Coordinate(current.x - 1, current.y);
        case 'S':
            return new Coordinate(current.x + 1, current.y);
        case 'E':
            return new Coordinate(current.x, current.y + 1);
        case 'W':
            return new Coordinate(current.x, current.y - 1);
        default:
            return current;
    }
}

private static boolean isWithinGrid(Coordinate pos) {
    return pos.x >= 0 && pos.x < n && pos.y >= 0 && pos.y < m;
}

private static String readResponse() {
    Scanner in = new Scanner(System.in);
    return in.nextLine();
}

private static void updateTeleport(Coordinate pos1, Coordinate pos2) {
    int index1 = (pos1.x) * m + (pos1.y) + 1;
    int index2 = (pos2.x) * m + (pos2.y) + 1;
    teleportPairs.add(index1);
    teleportPairs.add(index2);
}

private static class Coordinate {
    int x, y;
    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

}

原文