用一个二维数组代码整个迷宫,0代表可以通过,1代表围墙,8代表走过的路径。
package com.tulun.unti;
import com.tulun.dao.MazeNode;
/**
* @ClassName Stack
* @Description 栈
* @Author lzq
* @Date 2018/11/7 20:06
* @Version 1.0
**/
public class Stack {
int top;
MazeNode[] mazeNodes;
public Stack() {
this(10);
}
public Stack(int number) {
this.mazeNodes = new MazeNode[number];
this.top = 0;
}
/**
* 入栈
* @param mazeNode
*/
public void push(MazeNode mazeNode) {
this.mazeNodes[top++] = mazeNode;
}
/**
* 出栈
*/
public void pop() {
this.top--;
}
/**
* 获得栈顶元素
* @return
*/
public MazeNode getTop() {
if (top >= 0) {
return this.mazeNodes[top];
}
return null;
}
}
package com.tulun.constant;
/**
* @ClassName Constant
* @Description 方向
* @Author lzq
* @Date 2018/11/7 20:29
* @Version 1.0
**/
public class Constant {
public static final int UP = 0; //上
public static final int DOWN = 1; //下
public static final int LEFT = 2; //左
public static final int RIGHT = 3; //右
public static final boolean GOABLE = true; //可以走
public static final boolean DONTGOABLE = false;
}
package com.tulun.dao;
import com.tulun.constant.Constant;
import com.tulun.unti.Stack;
import java.util.Scanner;
/**
* @ClassName Maze
* @Description 迷宫
* @Author lzq
* @Date 2018/11/7 20:26
* @Version 1.0
**/
public class Maze {
private int row;
private int col;
private MazeNode[][] mazeNodes;
private Stack stack;
public Maze(int row,int col) {
this.row = row;
this.col = col;
mazeNodes = new MazeNode[row][col];
stack = new Stack(row*col);
}
/**
* 添加结点
* @param x
* @param y
* @param value
*/
private void addMazeNodes(int x,int y,int value) {
mazeNodes[x][y] = new MazeNode(x,y,value);
}
/**
* 记录每个结点四周状态
*/
private void judgePath() {
for(int i = 0;i < mazeNodes.length;i++) {
for(int j = 0;j < mazeNodes[i].length;j++) {
if(mazeNodes[i][j].getValue() == 0) {
//上
if (i >= 1 && mazeNodes[i - 1][j].getValue() == 0) {
mazeNodes[i][j].getHashTable()[Constant.UP] = Constant.GOABLE;
}
//左
if (j >= 1 && mazeNodes[i][j - 1].getValue() == 0) {
mazeNodes[i][j].getHashTable()[Constant.LEFT] = Constant.GOABLE;
}
//右
if (j + 1 < mazeNodes[i].length && mazeNodes[i][j + 1].getValue() == 0) {
mazeNodes[i][j].getHashTable()[Constant.RIGHT] = Constant.GOABLE;
}
//下
if (i + 1 < mazeNodes.length && mazeNodes[i + 1][j].getValue() == 0) {
mazeNodes[i][j].getHashTable()[Constant.DOWN] = Constant.GOABLE;
}
}
}
}
}
/**
* 可以走记录路径
*/
public void goMaze(int i,int j) {
try {
if (i >= 0 && i < mazeNodes.length && j >= 0 && j < mazeNodes[i].length) {
if (mazeNodes[i][j].getHashTable()[Constant.RIGHT] == Constant.GOABLE) {
stack.push(mazeNodes[i][j]);
sign(i, j);
goMaze(i, ++j);
} else if (mazeNodes[i][j].getHashTable()[Constant.DOWN] == Constant.GOABLE) {
stack.push(mazeNodes[i][j]);
sign(i, j);
goMaze(++i, j);
} else if (mazeNodes[i][j].getHashTable()[Constant.LEFT] == Constant.GOABLE) {
stack.push(mazeNodes[i][j]);
sign(i, j);
goMaze(i, --j);
} else if (mazeNodes[i][j].getHashTable()[Constant.UP] == Constant.GOABLE) {
stack.push(mazeNodes[i][j]);
sign(i, j);
goMaze(--i, j);
} else {
if (i == mazeNodes.length - 1 && j == mazeNodes[i].length - 1) {
System.out.println("找到路径如下:");
sign(i, j);
show();
return;
} else {
stack.pop();
int x = stack.getTop().getX();
int y = stack.getTop().getY();
new_sign(x, y, i, j);
i = x;
j = y;
goMaze(i, j);
}
}
}
}catch (NullPointerException e) {
System.out.println("没有出路,重新输入!");
start();
}
}
/**
* 标记已走过的路,改变周围结点相应方向的状态
* @param i
* @param j
*/
private void sign(int i,int j) {
mazeNodes[i][j].setValue(8);
if(mazeNodes[i][j].getValue() != 1) {
if (i - 1 >= 0) { //上面结点的下面不能走了
mazeNodes[i - 1][j].getHashTable()[Constant.DOWN] = Constant.DONTGOABLE;
}
if (j - 1 >= 0) {
mazeNodes[i][j - 1].getHashTable()[Constant.RIGHT] = Constant.DONTGOABLE;
}
if (j + 1 < mazeNodes[i].length) {
mazeNodes[i][j + 1].getHashTable()[Constant.LEFT] = Constant.DONTGOABLE;
}
if (i + 1 < mazeNodes.length) {
mazeNodes[i + 1][j].getHashTable()[Constant.UP] = Constant.DONTGOABLE;
}
}
}
/**
* 走过走不通的结点重置它
* @param x
* @param y
* @param i
* @param j
*/
private void new_sign(int x,int y,int i,int j) {
mazeNodes[i][j].setValue(0); //重置结点
int lift_right = y - j;
int up_down = x - i;
if(lift_right == -1) { //在右边
mazeNodes[x][y].getHashTable()[Constant.RIGHT] = Constant.DONTGOABLE;
}else if(lift_right == 1){
mazeNodes[x][y].getHashTable()[Constant.LEFT] = Constant.DONTGOABLE;
}
if(up_down == -1) { //在下面
mazeNodes[x][y].getHashTable()[Constant.DOWN] = Constant.DONTGOABLE;
}else if(up_down == 1){
mazeNodes[x][y].getHashTable()[Constant.UP] = Constant.DONTGOABLE;
}
}
/**
* 打印迷宫
*/
public void show() {
for(int i = 0;i < mazeNodes.length;i++) {
for(int j = 0;j < mazeNodes[i].length;j++) {
System.out.print(mazeNodes[i][j].getValue()+" ");
}
System.out.println();
}
}
public void start() {
Scanner scanner = new Scanner(System.in);
System.out.println("请添加迷宫节点:");
int value;
for(int i = 0;i < row;i++) {
for(int j = 0;j < col;j++) {
value = scanner.nextInt();
addMazeNodes(i,j,value);
}
}
System.out.println("你输入的迷宫:");
show();
judgePath();
goMaze(0,0);
}
}
/**
* @ClassName MazeNode
* @Description 迷宫节点
* @Author lzq
* @Date 2018/11/7 20:26
* @Version 1.0
**/
public class MazeNode {
private int x;
private int y;
private int value;
private boolean[] hashTable;
public MazeNode(int x,int y,int value) {
this.x = x;
this.y = y;
this.value = value;
this.hashTable = new boolean[4];
}
public boolean[] getHashTable() {
return hashTable;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
package com.tulun.main;
import com.tulun.dao.Maze;
import java.util.Scanner;
/**
* @ClassName TestDemo3
* @Description 测试
* @Author lzq
* @Date 2018/11/7 21:10
* @Version 1.0
**/
public class TestDemo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入迷宫大小");
System.out.print("长:");
int row = scanner.nextInt();
System.out.print("宽:");
int col = scanner.nextInt();
System.out.println();
Maze maze = new Maze(row,col);
maze.start();
}
}
请输入迷宫大小
长:4
宽:5
请添加迷宫节点:
0 0 1 0 0
0 0 0 0 0
0 1 0 1 1
1 0 0 0 0
你输入的迷宫:
0 0 1 0 0
0 0 0 0 0
0 1 0 1 1
1 0 0 0 0
找到路径如下:
8 8 1 0 0
0 8 8 0 0
0 1 8 1 1
1 0 8 8 8