📅  最后修改于: 2023-12-03 15:23:04.373000             🧑  作者: Mango
这是一道算法题,题目如下:
给定一个长度为N的迷宫矩阵,其中"."为路,"#"为墙。现在要求从起点(0,0)到终点(N-1,N-1),输出任意一条可行路径,如果不存在则输出"-1"。
注意:两个点相邻的条件为在同一行或同一列上且相隔距离为1。
该题可以使用广度优先搜索算法(BFS)来解决。从起点开始,不断遍历每一格,并标记从起点到该格的距离。当搜索到终点时,检查是否存在从起点到终点的路径,如果有,输出该路径。
具体步骤如下:
from collections import deque
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 上下左右四个方向
def bfs(maze):
n = len(maze)
visited = [[False] * n for _ in range(n)] # 初始化visited数组
dist = [[0] * n for _ in range(n)] # 初始化dist数组
q = deque([(0, 0)]) # 将起点加入队列中
visited[0][0] = True
while q:
x, y = q.popleft()
if x == n-1 and y == n-1: # 如果到达了终点,则回溯路径并返回
path = [(n-1, n-1)]
while path[-1] != (0, 0):
for dx, dy in dirs:
nx, ny = path[-1][0] + dx, path[-1][1] + dy
if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] and dist[nx][ny] == dist[path[-1][0]][path[-1][1]] - 1:
path.append((nx, ny))
break
path.append((0, 0))
return path[::-1]
for dx, dy in dirs:
nx, ny = x + dx, y + dy
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny] and maze[nx][ny] == ".":
visited[nx][ny] = True
dist[nx][ny] = dist[x][y] + 1
q.append((nx, ny))
return -1 # 没有到达终点
// 添加Java代码
import java.util.*;
public class Main {
static int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 上下左右四个方向
public static List<Pair<Integer, Integer>> bfs(char[][] maze) {
int n = maze.length;
boolean[][] visited = new boolean[n][n]; // 初始化visited数组
int[][] dist = new int[n][n]; // 初始化dist数组
Queue<Pair<Integer, Integer>> q = new LinkedList<>(); // 创建队列
q.offer(new Pair<>(0, 0)); // 将起点加入队列中
visited[0][0] = true;
while (!q.isEmpty()) {
Pair<Integer, Integer> cur = q.poll();
int x = cur.getKey(), y = cur.getValue();
if (x == n - 1 && y == n - 1) { // 如果到达了终点,则回溯路径并返回
List<Pair<Integer, Integer>> path = new ArrayList<>();
path.add(new Pair<>(n - 1, n - 1));
while (!path.get(path.size() - 1).equals(new Pair<>(0, 0))) {
for (int[] dir : dirs) {
int nx = path.get(path.size() - 1).getKey() + dir[0], ny = path.get(path.size() - 1).getValue() + dir[1];
if (0 <= nx && nx < n && 0 <= ny && ny < n && visited[nx][ny] && dist[nx][ny] == dist[path.get(path.size() - 1).getKey()][path.get(path.size() - 1).getValue()] - 1) {
path.add(new Pair<>(nx, ny));
break;
}
}
}
path.add(new Pair<>(0, 0));
Collections.reverse(path);
return path;
}
for (int[] dir : dirs) {
int nx = x + dir[0], ny = y + dir[1];
if (0 <= nx && nx < n && 0 <= ny && ny < n && !visited[nx][ny] && maze[nx][ny] == '.') {
visited[nx][ny] = true;
dist[nx][ny] = dist[x][y] + 1;
q.offer(new Pair<>(nx, ny));
}
}
}
return null; // 没有到达终点
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
char[][] maze = new char[n][n];
for (int i = 0; i < n; i++) {
String line = in.next();
for (int j = 0; j < n; j++) {
maze[i][j] = line.charAt(j);
}
}
List<Pair<Integer, Integer>> path = bfs(maze);
if (path == null) {
System.out.println(-1);
} else {
for (Pair<Integer, Integer> p : path) {
System.out.println(p.getKey() + " " + p.getValue());
}
}
}
}
对于一个N * N的迷宫,BFS的时间复杂度为O(N^2),空间复杂度为O(N^2)。因此,该算法的时间和空间效率均较高。