文章目录
- 一、题目
- 🔸题目描述
- 🔸输入输出
- 🔸样例1
- 🔸样例2
- 二、代码参考
- 作者:KJ.JK
🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
🍂个人博客首页: KJ.JK
💖系列专栏:华为OD机试(Java&Python&C语言)
一、题目
🔸题目描述
定义一个二维数组 N*M ,如 5 × 5 数组下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的路线。入口点为[0,0],既第一格是可以走的路。
🔸输入输出
输入
输入两个整数,分别表示二维数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道
输出
左上角到右下角的最短路径,格式如样例所示
🔸样例1
输入
5 5
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
输出
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)
🔸样例2
输入
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 1
0 1 1 1 0
0 0 0 0 0
输出
(0,0)
(1,0)
(2,0)
(3,0)
(4,0)
(4,1)
(4,2)
(4,3)
(4,4)
说明:
注意:不能斜着走!!
二、代码参考
import java.util.*;
// 题目已经提示了 【迷宫只有一条通道】,则直接使用 DFS 找路径就行了,如不有多条路径找最短考虑使用 BFS
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int m = in.nextInt();
// 构造迷宫
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = in.nextInt();
}
}
// 路径存储的数组
List<Pos> path = new ArrayList<>();
// DFS 搜索路径
dfs(map, 0, 0, path);
// 输出
for (Pos p : path) {
System.out.println("(" + p.x + "," + p.y + ")");
}
}
}
// 返回值 标记是否找到可通行的路劲
public static boolean dfs(int[][] map, int x, int y, List<Pos> path) {
// 添加路径并标记已走
path.add(new Pos(x, y));
map[x][y] = 1;
// 结束标志
if (x == map.length - 1 && y == map[0].length - 1) {
return true;
}
// 向下能走时
if (x + 1 < map.length && map[x + 1][y] == 0) {
if (dfs(map, x + 1, y, path)) {
return true;
}
}
// 向右能走时
if (y + 1 < map[0].length && map[x][y + 1] == 0) {
if (dfs(map, x, y + 1, path)) {
return true;
}
}
// 向上能走时
if (x - 1 > -1 && map[x - 1][y] == 0) {
if (dfs(map, x - 1, y, path)) {
return true;
}
}
// 向左能走时
if (y - 1 > -1 && map[x][y - 1] == 0) {
if (dfs(map, x, y - 1, path)) {
return true;
}
}
// 回溯
path.remove(path.size() - 1);
map[x][y] = 0;
return false;
}
// 简单的位置类
public static class Pos {
int x;
int y;
public Pos(int x, int y) {
this.x = x;
this.y = y;
}
}
}
--------------------------------------------------------
while True:
try:
m, n = list(map(int, input().split()))
maze = []
for _ in range(m):
maze.append(list(map(int, input().split())))
def walk(i, j, pos=[(0, 0)]):
if j+1 < n and maze[i][j+1] == 0: # 向右
if (i, j+1) not in pos:
walk(i, j+1, pos + [(i, j+1)])
if j-1 >= 0 and maze[i][j-1] == 0: # 向左
if (i, j-1) not in pos:
walk(i, j-1, pos + [(i, j-1)])
if i+1 < m and maze[i+1][j] == 0: # 向下
if (i+1, j) not in pos:
walk(i+1, j, pos + [(i+1, j)])
if i-1 >= 0 and maze[i-1][j] == 0: # 向上
if (i-1, j) not in pos:
walk(i-1, j, pos + [(i-1, j)])
if (i, j) == (m-1, n-1): # 到达出口
for p in pos:
print('(' + str(p[0]) + ',' + str(p[1]) + ')')
walk(0, 0)
except:
break
--------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int trace[100][2] = {0}, // 记录路径
min_trace[100][2] = {0}; // 记录最短路径
int maze[10][10]; // 迷宫
// int curStep = 0; // 表示当前前进的步数,也是最终的路径长度
int minStep = 100; // 最短路径
int flag = 0;
int findTrace(int curXidx, int curYidx, int row, int col, int curStep);
int main() {
int row, col;
while (scanf("%d %d", &row, &col) != EOF) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
scanf("%d", &maze[i][j]);
}
}
flag = findTrace(0, 0, row, col, 0);
if (flag == -1) {
printf("ERROR\n");
} else {
for (int i = 0; i <= minStep; i++) {
printf("(%d,%d)\n", min_trace[i][0], min_trace[i][1]);
}
}
}
return 0;
}
int findTrace(int curXidx, int curYidx, int row, int col, int curStep) {
int f = 0;
// 到达终点
if (curXidx == row - 1 && curYidx == col - 1) {
trace[curStep][0] = curXidx;
trace[curStep][1] = curYidx;
if (curStep < minStep) { // 复制最短路径
minStep = curStep;
for (int i = 0; i <= minStep; i++) {
min_trace[i][0] = trace[i][0];
min_trace[i][1] = trace[i][1];
}
}
return 1;
}
// 该点可达
if ((curXidx >= 0 && curXidx < row ) &&
(curYidx >= 0 && curYidx < col) &&
(maze[curXidx][curYidx] == 0)) {
trace[curStep][0] = curXidx;
trace[curStep][1] = curYidx;
++curStep;
maze[curXidx][curYidx] = -1; // 表示走过
f = findTrace(curXidx + 1, curYidx, row, col, curStep) ||
findTrace(curXidx, curYidx + 1, row, col, curStep) ||
findTrace(curXidx, curYidx - 1, row, col, curStep) ||
findTrace(curXidx - 1, curYidx, row, col, curStep);
maze[curXidx][curYidx] = 0; // 恢复
}
// 此路不通
else {
return 0;
}
return f;
}
作者:KJ.JK
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习