package com.algorithm.algorithm01server.demo;
import java.util.*;
public class Demo5 {
//图论 F 迷宫问题
static int wide, high, step, Row, Col;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static int[][] map;
static int target = 11;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List list = new ArrayList();
while (true) {
wide = scanner.nextInt();
high = scanner.nextInt();
if (high == 0 || wide == 0) {
break;
}
//二维数组存储邻接矩阵
map = new int[high][wide];
for (int i = 0; i < high; i++) {
for (int j = 0; j < wide; j++) {
map[i][j] = scanner.nextInt();
if (map[i][j] == 2) {
Row = i;
Col = j;
}
}
}
//最开始的地方走了一步
step = 1;
int dfs = dfs(Row, Col, step);
list.add(dfs);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
private static int dfs(int Row, int Col, int step) {
if (step > 10) {
return -1;
}
//找到返回步数
for (int i = 0; i < 4; i++) {
//走的方向
int nx = Row + dx[i];
int ny = Col + dy[i];
//如果没有碰到墙继续向前走
while (nx >= 0 && nx < high && ny >= 0 && ny < wide && map[nx][ny] != 1) {
if (map[nx][ny] == 3) {
return Math.min(step, target);
}
nx += dx[i];
ny += dy[i];
}
if (nx >= 0 && nx < high && ny >= 0 && ny < wide && map[nx][ny] == 1) {
map[nx][ny] = 0;//碰到石块,坐标的位置标记为0
dfs(nx - dx[i], ny - dy[i], step + 1);//找到石块坐标的前一个位置,然后步数+1
map[nx][ny] = 1;
}
}
//没有找到返回-1
return -1;
}
}
dfs的一个问题,题目大概是:如果遇到的格子是 0空置广场 1是堵塞 2是开始位置 3目标位置,求最小步数问题。
然后我每次输入测试用例的时候都返回的是-1,找了很久没发现问题,请求一下,问问是哪里写错了?
我的测试用例
6 1
1 1 2 1 1 3
0 0
5*5迷宫初始化如下
0 0 1 0 1
0 0 0 0 1
0 0 1 0 0
0 1 0 0 1
0 0 0 0 1
初始位置在(0,0)处,欲到达(3,2)处最少需要走多少步
代码如下:
1 package DFSearch;
2
3 import java.util.Scanner;
4
5 public class FindPath {
6 //人质所在迷宫的位置
7 static int fx,fy;
8 //迷宫为5*5
9 static int n=5;
10 //上下左右移动
11 static int[][] temp ={{0,1},{1,0},{0,-1},{-1,0}};
12 //迷宫数组
13 static int [][] squera = new int [n][n];
14 //标记数组,走过就标记为1
15 static int [][] book = new int [n][n];
16 //最短步数
17 static int min = 9999999;
18 public static void main(String[] args){
19 @SuppressWarnings("resource")
20 Scanner scan = new Scanner(System.in);
21
22 System.out.println("请输入迷宫5*5:");
23 for(int i=0;i<n;i++){
24 for(int j=0;j<n;j++){
25 squera[i][j] = scan.nextInt();
26 }
27 }
28 System.out.println("请输入人质所在位置:");
29 fx = scan.nextInt();
30 fy = scan.nextInt();
31 book[0][0] = 1;
32 dfs(0,0,0);
33 System.out.println(min);
34 /*for(int i=0;i<5;i++){
35 for(int j=0;j<5;j++){
36 if(book[i][j]==1){
37 System.out.println("<"+i+","+j+">->");
38 }
39 }
40 }*/
41 }
42 public static void dfs(int x,int y,int step){
43 //如果到达地点,结束
44 if(x==fx&&y==fy){
45 if(step<min){
46 min = step;
47 }
48 return;
49 }
50 //循环移动到四个方向
51 for(int i=0;i<4;i++){
52 int tx = temp[i][0];
53 int ty = temp[i][1];
54 //如果该方向越界了,改变到另一个方向
55 if(x+tx<0||x+tx>=n)
56 continue;
57 if(y+ty<0||y+ty>=n)
58 continue;
59 //如果该位置没有障碍物并且也没有走过,走
60 if(squera[x+tx][y+ty]==0 && book[x+tx][y+ty]==0){
61 //标记为走过
62 book[x+tx][y+ty] = 1;
63 //搜索过程
64 //System.out.println(""+(x+tx)+","+(y+ty)+"->");
65 //往下一层递归
66 dfs(x+tx,y+ty,step+1);
67 //取消标记,回到上一层
68 book[x+tx][y+ty] = 0;
69 }
70 }
71 return;
72 }
73 }
执行结果:
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632