深度优先实现了一个走迷宫程序
运行:
原始迷宫:
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 1 0 1
1 1 1 0 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
路径(从终点到原点打印):
[(6,5), (5,5), (4,5), (4,4), (4,3), (3,3), (2,3), (1,3), (1,2), (1,1)]
算法走过的痕迹:
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 0 0 2 1 2 1
1 1 1 2 1 1 1
1 0 0 2 2 2 1
1 0 0 0 0 2 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
代码:
package cn.agan.recursive;
import java.util.Objects;
import java.util.Stack;
/**
* 递归解决迷宫问题:
* 深度优先走迷宫
*/
public class MiGong {
public static void main(String[] args) {
Map map = new Map(8, 7);
map.setLimit1(); //设置挡板
System.out.println("原始迷宫:");
map.show();
map.setWay(1,1, 6, 5);
// map.show();
}
}
//地图类
class Map {
//二维数组模拟迷宫
private int[][] map;
private int row;
private int col;
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public Map(int row, int col) {
this.row = row;
this.col = col;
map = new int[row][col];
//1表示墙,上下都是墙
for(int i = 0; i < col; i++) {
map[0][i] = 1;
map[row-1][i] = 1;
}
//左右两边也是墙
for (int i = 0; i < row; i++) {
map[i][0] = 1;
map[i][col-1] = 1;
}
}
/**
* 查找路线
* @param startI 起始地址 行
* @param startJ 起始地址 列
* @param endI 目的地 行
* @param endJ 目的地 列
* @return 返回TRUE说明能够找到路径, 返回false表示不能够找到路径
*/
public boolean setWay(int startI, int startJ, int endI, int endJ) {
Map map1 = new Map(this);
Stack<Point> res = new Stack<>();
Point start = new Point(startI, startJ);
Point end = new Point(endI, endJ);
boolean wayFouned = map1.findWay(res, start, end);
if (wayFouned) {
System.out.println("路径(从终点到原点打印):");
System.out.println(res);
} else {
System.out.println("走不到终点");
}
System.out.println("算法走过的痕迹:");
map1.show();
return true;
}
public boolean findWay(Stack<Point> res, Point cur, Point end) {
if (cur.equals(end)) {
res.push(cur);
return true;
}
if (pointVal(cur) == 1 || pointVal(cur) == 2 ) {
return false;
}
if ( pointVal(cur) == 0 ) {
setPointVal(cur, 2);
}
for (int i = 0; i < 4; i++) {
Point next = new Point(cur, i);
if (findWay(res, next, end)) {
res.push(cur);
return true;
}
}
return false;
}
public int pointVal(Point p) {
return map[p.getRow()][p.getCol()];
}
public void setPointVal(Point p, int val) {
map[p.getRow()][p.getCol()] = val;
}
//拷贝一份地图
public Map(Map m) {
this.row = m.row;
this.col = m.col;
this.map = new int[this.row][this.col];
for (int i = 0; i < this.row; i++) {
for (int j = 0; j < this.col; j++ ) {
this.map[i][j] = m.map[i][j];
}
}
}
//一种设置挡板的方案
public void setLimit1() {
map[3][1] = 1;
map[3][2] = 1;
map[3][4] = 1;
map[3][5] = 1;
map[2][4] = 1;
}
public void show() {
if (map == null) {
System.out.println("地图是空的");
return;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.printf("%d\t", map[i][j]);
}
System.out.println();
}
}
}
class Point {
private int row;
private int col;
public Point(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public String toString() {
return "(" + row +
"," + col +
')';
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return row == point.row &&
col == point.col;
}
public Point(Point p, int direct) {
switch (direct) {
case 0 ://"up":
row = p.getRow()-1;
col = p.getCol();
break;
case 1: //"left":
row = p.getRow();
col = p.getCol()+1;
break;
case 2: //"down":
row = p.getRow()+1;
col = p.getCol();
break;
case 3: //"right":
row = p.getRow();
col = p.getCol()-1;
break;
default:
}
}
}