Java实现走迷宫回溯算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Java实现⾛迷宫回溯算法
以⼀个M×N的长⽅阵表⽰迷宫,0和1分别表⽰迷宫中的通路和障碍。

设计⼀个程序,对任意设定的迷宫,求出⼀条从⼊⼝到出⼝的通路,或得出没有通路的结论。

(1)根据⼆维数组,输出迷宫的图形。

(2)探索迷宫的四个⽅向:RIGHT为向右,DOWN向下,LEFT向左,UP向上,输出从⼊⼝到出⼝的⾏⾛路径。

例⼦:
左上⾓(1,1)为⼊⼝,右下⾓(8,9)为出⼝。

可使⽤回溯⽅法,即从⼊⼝出发,顺着某⼀个⽅向进⾏探索,若能⾛通,则继续往前进;否则沿着原路退回,换⼀个⽅向继续探索,直⾄出⼝位置,求得⼀条通路。

假如所有可能的通路都探索到⽽未能到达出⼝,则所设定的迷宫没有通路。

import java.util.*;
class Position{
public Position(){
}
public Position(int row, int col){
this.col = col;
this.row = row;
}
public String toString(){
return "(" + row + " ," + col + ")";
}
int row;
int col;
}
class Maze{
public Maze(){
maze = new int[15][15];
stack = new Stack<Position>();
p = new boolean[15][15];
}
/*
* 构造迷宫
*/
public void init(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输⼊迷宫的⾏数");
row = scanner.nextInt();
System.out.println("请输⼊迷宫的列数");
col = scanner.nextInt();
System.out.println("请输⼊" + row + "⾏" + col + "列的迷宫");
int temp = 0;
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j) {
temp = scanner.nextInt();
maze[i][j] = temp;
p[i][j] = false;
}
}
}
/*
* 回溯迷宫,查看是否有出路
*/
public void findPath(){
// 给原始迷宫的周围家⼀圈围墙
int temp[][] = new int[row + 2][col + 2];
for(int i = 0; i < row + 2; ++i) {
for(int j = 0; j < col + 2; ++j) {
temp[0][j] = 1;
temp[row + 1][j] = 1;
temp[i][0] = temp[i][col + 1] = 1;
}
}
// 将原始迷宫复制到新的迷宫中
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j) {
temp[i + 1][j + 1] = maze[i][j];
}
}
// 从左上⾓开始按照顺时针开始查询
int i = 1;
int j = 1;
p[i][j] = true;
stack.push(new Position(i, j));
while (!stack.empty() && (!(i == (row) && (j == col)))) { if ((temp[i][j + 1] == 0) && (p[i][j + 1] == false)) {
p[i][j + 1] = true;
stack.push(new Position(i, j + 1));
j++;
} else if ((temp[i + 1][j] == 0) && (p[i + 1][j] == false)) { p[i + 1][j] = true;
stack.push(new Position(i + 1, j));
i++;
} else if ((temp[i][j - 1] == 0) && (p[i][j - 1] == false)) { p[i][j - 1] = true;
stack.push(new Position(i, j - 1));
j--;
} else if ((temp[i - 1][j] == 0) && (p[i - 1][j] == false)) { p[i - 1][j] = true;
stack.push(new Position(i - 1, j));
i--;
} else {
stack.pop();
if(stack.empty()){
break;
}
i = stack.peek().row;
j = stack.peek().col;
}
}
Stack<Position> newPos = new Stack<Position>();
if (stack.empty()) {
System.out.println("没有路径");
} else {
System.out.println("有路径");
System.out.println("路径如下:");
while (!stack.empty()) {
Position pos = new Position();
pos = stack.pop();
newPos.push(pos);
}
}
/*
* 图形化输出路径
* */
String resault[][]=new String[row+1][col+1]; for(int k=0;k<row;++k){
for(int t=0;t<col;++t){
resault[k][t]=(maze[k][t])+"";
}
}
while (!newPos.empty()) {
Position p1=newPos.pop();
resault[p1.row-1][p1.col-1]="#";
}
for(int k=0;k<row;++k){
for(int t=0;t<col;++t){
System.out.print(resault[k][t]+"\t");
}
System.out.println();
}
}
int maze[][];
private int row = 9;
private int col = 8;
Stack<Position> stack;
boolean p[][] = null;
}
class hello{
public static void main(String[] args){
Maze demo = new Maze();
demo.init();
demo.findPath();
}
}
运⾏⽰例:
请输⼊迷宫的⾏数
3
请输⼊迷宫的列数
3
请输⼊3⾏3列的迷宫
0 1 1
0 0 1
1 0 0
有路径
路径如下:
请输⼊迷宫的⾏数
9
请输⼊迷宫的列数
8
请输⼊9⾏8列的迷宫
0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0
0 0 1 0 1 1 0 1
0 1 1 1 0 0 1 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 1
0 1 1 1 1 0 0 1
1 1 0 0 0 1 0 1
1 1 0 0 0 0 0 0
有路径
路径如下:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

相关文档
最新文档