关注
走迷宫Java代码个人整理,供有需要的人参考。(可以求出所有路径和最优路径,以及最优路径下所耗的能量)
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
class Pair{
public int x;
public int y;
public Pair(int x,int y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static Stack<Pair> res = new Stack<>();
public static int[][] book;//用来标记改点是否走过,走过为1,没走为0
public static int[][] arr;//测试数组
public static int max = 0;
public static Map<Integer, Stack<Pair>> map = new HashMap<>();//存放路径
public static void main(String ars[]) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int P = s.nextInt();
arr = new int[n][m];// 初始化数组
book = new int[n][m];// 初始化数组
for (int i = 0; i < n; i++)// 循环输入
for (int j = 0; j < m; j++)
arr[i][j] = s.nextInt();
Stack<Pair> path = new Stack<>();
dfs(path,0,0,P);
if (map.size() == 0) {
System.out.println("Can not escape!");
}else {
System.out.println("可以找到最优路径如下所示:");
System.out.print("[0,0]");
Iterator<Pair> iter = map.get(max).iterator();
while (iter.hasNext()) {
Pair pairTmp = iter.next();
System.out.print(",["+pairTmp.x+","+pairTmp.y+"]");
}
System.out.println("\n最优路径所耗能量为:"+(P-max));
}
}
public static void dfs(Stack<Pair> path, int x, int y, int P) {
int n = arr.length;
int m = arr[0].length;
int next[][] = {
{0,1},
{1,0},
{0,-1},
{-1,0}
};//定义四个方向(分别是向右、向下、向左、向右) if (P <= 0 && x != 0 && y != m-1) {
return;
}
//找到符合条件的路径
if (x == 0 && y == m-1) {
if (P>=0) {
Stack<Pair> pathTmp = new Stack<>();
Iterator<Pair> iter = path.iterator();
while (iter.hasNext()) {
pathTmp.add(iter.next());
}
map.put(P, pathTmp);
}
if (max < P) {//判断是否为最优路径
max = P;
}
return;
}
int tx,ty;
//枚举四种走法
for (int k = 0; k <= 3; k++) {
tx = x+next[k][0];
ty = y+next[k][1];
//判断是否越界
if (tx < 0 || tx >= n || ty < 0 || ty >= m) {
continue;
}
if (arr[tx][ty] == 1 && book[tx][ty] == 0) { if (k == 0 || k==2) {//如果是水平方向走
book[tx][ty] = 1;
path.add(new Pair(tx, ty));
dfs(path, tx, ty, P-1);
path.pop();
book[tx][ty] = 0;
}
if (k==1) {//如果是向下走 book[tx][ty] = 1;
path.add(new Pair(tx, ty));
dfs(path, tx, ty, P);
path.pop();
book[tx][ty] = 0;
}
if (k==3) {//如果是向上走 book[tx][ty] = 1;
path.add(new Pair(tx, ty));
dfs(path, tx, ty, P-3);
path.pop();
book[tx][ty] = 0;
}
}
}
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
09-21 14:14
华东交通大学 单片机 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# HR问:你期望的薪资是多少?如何回答 #
67307次浏览 653人参与
# 我和mentor的爱恨情仇 #
77922次浏览 431人参与
# “vivo”个offer #
41728次浏览 288人参与
# 打工人的精神状态 #
98606次浏览 1289人参与
# 被同事甩锅了怎么办 #
24231次浏览 100人参与
# 你见过哪些工贼行为 #
28566次浏览 141人参与
# 一人一个landing小技巧 #
125611次浏览 1457人参与
# 你的秋招第一场笔试是哪家 #
258738次浏览 2026人参与
# 满帮集团求职进展汇总 #
6743次浏览 68人参与
# 考研失败就一定是坏事吗? #
149701次浏览 1081人参与
# vivo工作体验 #
29288次浏览 125人参与
# 和mentor 1on1 都聊什么? #
2315次浏览 21人参与
# 哪一瞬间觉得自己长大了 #
39558次浏览 494人参与
# 上班后和你想的一样吗? #
88119次浏览 671人参与
# 校招生月薪1W算什么水平 #
43458次浏览 235人参与
# 实习吐槽大会 #
386728次浏览 2156人参与
# 如何准备秋招 #
64716次浏览 830人参与
# 你想留在一线还是回老家? #
62793次浏览 537人参与
# 工作后明白的那些道理 #
23010次浏览 225人参与
# 数字马力求职进展汇总 #
206574次浏览 1636人参与
# 我是面试官,请用一句话让我破防 #
29049次浏览 133人参与

