java迷宫项目
项目介绍:
一个网格迷宫由n行m列的单元格组成,每个大院要么是空地(用0表示),要么是障碍物(用1表示)。你的任务是找一条从起点到终点的移动序列,其中只能上下左右移动到相邻单元格。任何时候都不能在有障碍物的单元格中,也不能走到迷宫之外。起点为左上角和终点右下角。
项目功能:
解决迷宫路径查找问题,寻找一条从左上角迷宫入口到右下角迷宫出口的一条有效路径,0代表可走,1代表不能行走,找到请输出最终的迷宫和路径信息,找不到请输出不存在有效路径。
项目所用知识点:
采用Java面向对象思想,二维数组以及非递归栈进行实现
项目实现思路:
- 定义一个迷宫节点类型(MazeNode)的二维数组
- 初始化每个格子中的value值。给二维数组每个格子存放对象。对象的value值只能为0(当前格子可以走)或者1(当前格子不能走)
- 初始化每个格子四个方向(东南西北)是否是可走状态。根据当前节点周围四个方向格子中的value值,判断当前节点的东南西北四个方向是否可走(0是可走,1不可走)。
- 开始走迷宫。采用栈操作,记录行走的路径,将左上角元素入栈,判断当前栈顶元素的哪个方向可走,将其中一个可走方向进行入栈操作,直到右下角元素停止。栈中保存走过的路径。 注意: 如果遇到走入死胡同问题,此时需要将是栈顶元素并且栈顶元素的四个方向都不能行走,此时将其出栈,选择新方向再次入栈,直到右下角元素停止。
public class Maze{
private MazeNode[][] mazeNodes;
private int row;
private int colum;
private MazeStack stack;
public Maze(int row, int colum) {
this.row = row;
this.colum = colum;
stack = new MazeStack();
mazeNodes = new MazeNode[this.row][this.colum];
}
public void setMazeNode(int i, int j, int value) {
mazeNodes[i][j] = new MazeNode(i, j, value);
}
public void goMaze() {
if (mazeNodes[0][0].value != 0) {
System.out.println("没有路可走");
return;
}
initWayState();
int i = 0;
int j = 0;
stack.push(mazeNodes[i][j]);
while (!stack.empty()) {
i = stack.top().getX(); //获得栈顶元素的行列号
j = stack.top().getY();
if (i == this.row - 1 && j == this.colum - 1) {
break;
}
if (mazeNodes[i][j].way_east == true) {
mazeNodes[i][j].way_east = false;
mazeNodes[i][j + 1].way_west = false;
//把走过的路封掉
stack.push(mazeNodes[i][j + 1]);
continue;
}
if (mazeNodes[i][j].way_west == true) {
mazeNodes[i][j].way_west = false;
mazeNodes[i][j - 1].way_east = false;
stack.push(mazeNodes[i][j - 1]);
continue;
}
if (mazeNodes[i][j].way_north == true) {
mazeNodes[i][j].way_north = false;
mazeNodes[i - 1][j].way_south = false;
stack.push(mazeNodes[i - 1][j]);
continue;
}
if (mazeNodes[i][j].way_south == true) {
mazeNodes[i][j].way_south = false;
mazeNodes[i + 1][j].way_north = false;
stack.push(mazeNodes[i + 1][j]);
continue;
}
stack.pop();
}
if (stack.empty()) {
System.out.println("没有路径");
}
}
/**
* 寻找可走路径
*/
public void initWayState () {
for (int i = 0; i < row; ++i) {
for (int j = 0; j < colum; ++j) {
if (mazeNodes[i][j].value == 0) {
if (j + 1 < colum && mazeNodes[i][j + 1].value == 0) {
mazeNodes[i][j].way_east = true;
}
if (j - 1 >= 0 && mazeNodes[i][j - 1].value == 0) {
mazeNodes[i][j].way_west = true;
}
if (i - 1 >= 0 && mazeNodes[i - 1][j].value == 0) {
mazeNodes[i][j].way_north = true;
}
if (i + 1 < colum && mazeNodes[i + 1][j].value == 0) {
mazeNodes[i][j].way_south = true;
}
}
}
}
}
public void showMaze () {
while (!stack.empty()) {
int i = stack.top().getX(); //获得栈顶元素坐标
int j = stack.top().getY();
this.setMazeNode(i,j,2);
stack.pop();
}
System.out.println("迷宫路径信息(路径可以走用2表示):");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < colum; ++j) {
System.out.print(this.mazeNodes[i][j].getValue() + " ");
}
System.out.println();
}
}
}
设置迷宫节点
public class MazeNode {
public int value;
public boolean way_east;
public boolean way_west;
public boolean way_north;
public boolean way_south;
private int x;
private int y;
/**
* 节点对象的初始化
*/
public MazeNode(int i, int j,int value) {
this.x = i;
this.y = j;
this.value = value;
//初始化节点的四个方向行走信息的时候,都初始化成不能走
this.way_east = new Boolean("false");
this.way_west = new Boolean("false");
this.way_north = new Boolean("false");
this.way_south = new Boolean("false");
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getValue() {
return value;
}
}
设置迷宫所需栈
import java.util.Arrays;
public class MazeStack {
private MazeNode[] stack;
private int top;
public MazeStack(){
this.top = 0;
this.stack = new MazeNode[10];
}
/**
* 入栈
*/
public void push(MazeNode mazeNode) {
if(full()) {
this.stack=Arrays.copyOf(this.stack, 2*this.top);
}
this.stack[top++]=mazeNode;
}
/**
* 出栈
*/
public void pop() {
if(empty()) {
return;
}
this.top--;
}
/**
* 返回栈顶元素
*/
public MazeNode top() {
return this.stack[this.top-1];
}
/**
* 判断栈空
*/
public boolean empty() {
return this.top == 0;
}
/**
* 判断栈满
*/
public boolean full() {
return this.top == this.stack.length;
}
}
测试迷宫
public class MazeTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入迷宫大小:");
int row = in.nextInt();
int colum = in.nextInt();
Maze maze = new Maze(row,colum);
System.out.println("请输入迷宫具体的路径信息:0表示路径可以走,1表示路径不能走:");
for(int i=0; i<row; ++i){
for(int j=0; j<colum; ++j){
int value = in.nextInt();
maze.setMazeNode(i, j, value);
}
}
//开始寻找路径信息
maze.goMaze();
//打印最终的路径信息
maze.showMaze();
}
}