迷宫可以由二维数组构成,而数组中的每个元素可以是一个节点
节点类中有属性:
①节点的值
②横坐标③列坐标
④东⑤西⑥南⑦北(boolean数组)
⑧四个方向是否可以走
构造方法:初始化二维数组
常数接口:
二维数组的①行坐标②列坐标
节点的四个方向:①东②西③南 ④北
节点是否走的通:⑤是 ⑥否
迷宫类:
二维数组的①行数②列数
节点数组 ③ MazeNode[][] mazeNodes;
④栈
用于初始化每一个节点
方法①调整路径状态,看没有方向是否可以走
方法②走迷宫
判断第一个是否为墙
不为墙,入栈,否则,结束
当栈为空
方法③入栈操作,并且修改走过的路径
向东可以走
①设置栈顶元素的东边不可走
②设置栈顶元素的东边元素的西边走不通
③东边元素入栈
向南可以走
向西可以走
向北可以走
否则,删除栈顶元素
输出
import java.util.Scanner;
import java.util.Stack;
public class Maze {
private MazeNode[][] mazeNodes ;
private int row;
private int colum;
private Stack stack = new Stack<>();
public Maze(int row,int colum){
this.row = row;
this.colum = colum;
mazeNodes = new MazeNode[row][colum];
}
/**
* 从键盘上获取,用来初始化每一个节点
*/
public void initEveryMazeNode(){
Scanner scan = new Scanner(System.in);
for(int i = 0;i= 0 && mazeNodes[i][j-1].getVaule() == 0){
mazeNodes[i][j-1].setWayState(Contant.WAY_WEST,Contant.WAY_ABLE);
}
//北
if(i-1 >= 0 && mazeNodes[i-1][j].getVaule() == 0){
mazeNodes[i-1][j].setWayState(Contant.WAY_NORTH,Contant.WAY_ABLE);
}
}
}
}
/**
* 走迷宫
*/
public void goMaze(){
if(mazeNodes[0][0].getVaule() != 0){
System.out.println("没有迷宫路径");
return;
}
stack.push(mazeNodes[0][0]);
while (!stack.isEmpty()){
MazeNode top = stack.peek(); //获取栈顶元素
int i = top.getI(); //栈顶元素的横坐标
int j = top.getJ(); //栈顶元素的列坐标
if(i == row-1 && j == colum-1){
System.out.println("找到路径");
break;
}
//东
if(top.getWayState(Contant.WAY_EAST) == Contant.WAY_ABLE){
//设置本节点的东边为不可走
top.setWayState(Contant.WAY_EAST,Contant.WAY_DISABLE);
//设置东节点的西边为不可走
mazeNodes[i][j+1].setWayState(Contant.WAY_WEST,Contant.WAY_DISABLE);
stack.push(mazeNodes[i][j+1]); //入东边节点进栈
continue;
}
//南
else if(top.getWayState(Contant.WAY_SOUTH)== Contant.WAY_ABLE){
//设置本节点的南边不可走
top.setWayState(Contant.WAY_SOUTH,Contant.WAY_DISABLE);
//设置南节点的北边不可走
mazeNodes[i+1][j].setWayState(Contant.WAY_NORTH,Contant.WAY_DISABLE);
stack.push(mazeNodes[i+1][j]);
continue;
}
//西
else if(top.getWayState(Contant.WAY_WEST)== Contant.WAY_ABLE){
//设置本节点的西边不可走
top.setWayState(Contant.WAY_WEST,Contant.WAY_DISABLE);
//设置西节点的东边不可走
mazeNodes[i+1][j].setWayState(Contant.WAY_EAST,Contant.WAY_DISABLE);
stack.push(mazeNodes[i][j-1]);
continue;
}
//北
else if(top.getWayState(Contant.WAY_NORTH)== Contant.WAY_ABLE){
//设置本节点的北边不可走
top.setWayState(Contant.WAY_NORTH,Contant.WAY_DISABLE);
//设置北节点的南边不可走
mazeNodes[i+1][j].setWayState(Contant.WAY_SOUTH,Contant.WAY_DISABLE);
stack.push(mazeNodes[i+1][j]);
continue;
}else {
stack.pop();
}
}
}
public void show(){
/**
* 出栈
*/
while(!stack.isEmpty()){
System.out.println(stack.peek().getI()+" "+stack.peek().getJ());
stack.pop();
}
}
}
public class MazeNode implements Contant{
private int vaule; //
private int i; //横坐标
private int j; //列坐标
private boolean[] wayState = new boolean[Contant.INITSIZE]; //状态
public MazeNode(int vaule,int i,int j){
this.vaule = vaule;
this.i = i;
this.j = j;
}
public int getVaule() {
return vaule;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public boolean getWayState(int direction) {
return wayState[direction];
}
public void setWayState(int deriction,boolean isAble) {
wayState[deriction] = isAble;
}
}
public interface Contant {
int INITSIZE = 4;
int WAY_EAST = 0;
int WAY_SOUTH = 1;
int WAY_WEST = 2;
int WAY_NORTH = 3;
boolean WAY_ABLE = true;
boolean WAY_DISABLE = false;
}
public class TeseMaze {
public static void main(String[] args) {
Maze maze = new Maze(5,5);
maze.initEveryMazeNode();
maze.adjustWayState();
maze.goMaze();
maze.show();
}
}