问题描述:
/*
* 用非递归的栈来解决
* 用类来解决迷宫路径的查找问题,寻找一条从左上角迷宫入口
* 到右下角迷宫出口的一条有效路径,0代表可以行走,1代表不能行走,
* 找到,请输入最终的迷宫和路径信息, 找不到,请输出不存在有效路径。
*
* 软件的运行如下:
*
* 请输入迷宫的行列数(m * n):5 5
* 请输入迷宫的路径:
* 0 0 1 0 0
* 1 0 0 1 0
* 0 1 0 1 0
* 0 0 0 1 0
* 0 0 0 0 0
*
* 正在寻找迷宫路径。。。
*
* 从左上角入口到右下角出口不存在有效路径 、、、、
* 路径已找到,输入如下(2代表行走的路径):
* 2 2 1 0 0
* 1 2 2 1 0
* 0 1 2 1 0
* 0 0 2 1 0
* 0 0 2 2 2
*
*
* */
package com.src.maze;
//抽象迷宫路径的每一个节点的类型
public class MazeNode {
private int value;
private int i;
private int j;
private int[] pathState;
public MazeNode(int value, int i, int j){
this.value = value;
this.i = i;
this.j = j;
//初始化节点的四个方向的路径信息,都初始化成不能行走
pathState = new int[Constant.WAY_NUMBER];
for(int k=0; k//设置给节点的path方向状态为state
public void setPathState(int path,int state){
pathState[path] = state;
}
public int getPathState(int path){
return pathState[path];
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getRow() {
return i;
}
public void setRow(int i) {
this.i = i;
}
public int getCol() {
return j;
}
public void setCol(int j) {
this.j = j;
}
}
package com.src.maze;
//主要定义这个项目中所用到的所有的常量
public class Constant {
//表示方向的总数
public static final int WAY_NUMBER = 4;
//表示路径可以行走
public static final int WAY_ENABLE = 1;
//表示路径不可以行走
public static final int WAY_DISABLE = 0;
//表示迷宫节点四个方向
public static final int WAY_EAST = 0;
public static final int WAY_SOUTH = 1;
public static final int WAY_WEST = 2;
public static final int WAY_NORTH = 3;
}
package com.src.maze;
import java.util.Arrays;
import java.util.Scanner;
class SqStack{
private MazeNode[] stack;
private int top;
public SqStack(){
top = 0;
stack = new MazeNode[50];
}
//push要支持stack的内存增长操作,一次性增长2倍
public void push(MazeNode node){
if(full()){
this.resize();
}
this.stack[top++] = node;
}
public void pop(){
if(empty())
return ;
this.top--;
}
public MazeNode top(){
return this.stack[this.top-1];
}
public boolean full(){
return this.top==this.stack.length;
}
public boolean empty(){
return this.top == 0;
}
private void resize(){
this.stack = Arrays.copyOf(this.stack, this.stack.length*2);
}
}
//迷宫类
class Maze{
private int row;
private int colum;
private MazeNode[][] mazePath;
private SqStack stack;
public Maze(int row, int colum){
this.row = row;
this.colum = colum;
mazePath = new MazeNode[this.row][this.colum];
stack = new SqStack();
}
public void setPath(int i, int j, int value){
mazePath[i][j] = new MazeNode(value, i, j);
}
//该函数主要用来更改迷宫节点四个方向的行走状态
public void adjustMazePath(){
for(int i=0;ifor(int j=0;jif(mazePath[i][j].getValue() == 0){
//东
if(j1 && mazePath[i][j+1].getValue() == 0 ){
mazePath[i][j].setPathState(Constant.WAY_EAST, Constant.WAY_ENABLE);
}
//南
if(i1 && mazePath[i+1][j].getValue() == 0 ){
mazePath[i][j].setPathState(Constant.WAY_SOUTH
, Constant.WAY_ENABLE);
}
//西
if(j>0 && mazePath[i][j-1].getValue() == 0 ){
mazePath[i][j].setPathState(Constant.WAY_WEST, Constant.WAY_ENABLE);
}
//北
if(i>0 && mazePath[i-1][j].getValue() == 0 ){
mazePath[i][j].setPathState(Constant.WAY_NORTH, Constant.WAY_ENABLE);
}
}
}
}
}
//开始寻找迷宫路径
public void findMazePath(){
int i = 0;
int j = 0;
stack.push(mazePath[i][j]);
while(!stack.empty() ){
//获得栈顶元素的行列号。
i = stack.top().getRow();
j = stack.top().getCol();
if(i == row-1 && j == colum-1){
break;
}
//东,判断当前节点东边可以走,将当前节点的东边改为不可走,将东边节点的西边改为不可走,再将东边节点入栈,再进行下一次的循环判断
if(mazePath[i][j].getPathState(Constant.WAY_EAST) == Constant.WAY_ENABLE ){
mazePath[i][j].setPathState(Constant.WAY_EAST ,Constant.WAY_DISABLE);
mazePath[i][j+1].setPathState(Constant.WAY_WEST ,Constant.WAY_DISABLE);
stack.push(mazePath[i][j+1]);
continue;
}
//西
if(mazePath[i][j].getPathState(Constant.WAY_WEST) == Constant.WAY_ENABLE){
mazePath[i][j].setPathState(Constant.WAY_WEST ,Constant.WAY_DISABLE);
mazePath[i][j-1].setPathState(Constant.WAY_EAST ,Constant.WAY_DISABLE);
stack.push(mazePath[i][j-1]);
continue;
}
//南
if(mazePath[i][j].getPathState(Constant.WAY_SOUTH) == Constant.WAY_ENABLE){
mazePath[i][j].setPathState(Constant.WAY_SOUTH ,Constant.WAY_DISABLE);
mazePath[i+1][j].setPathState(Constant.WAY_NORTH ,Constant.WAY_DISABLE);
stack.push(mazePath[i+1][j]);
continue;
}
//北
if(mazePath[i][j].getPathState(Constant.WAY_NORTH) == Constant.WAY_ENABLE){
mazePath[i][j].setPathState(Constant.WAY_NORTH ,Constant.WAY_DISABLE);
mazePath[i-1][j].setPathState(Constant.WAY_SOUTH ,Constant.WAY_DISABLE);
stack.push(mazePath[i-1][j]);
continue;
}
stack.pop();
}
if(stack.empty()){
System.out.println("无合适路径");
}
}
//打印最终的迷宫路径信息
public void showMazePath(){
//将栈内元素改为2,即迷宫路径
while(!stack.empty()){
int i = stack.top().getRow();
int j = stack.top().getCol();
stack.pop();
this.setPath(i, j, 2 );
}
for(int i=0; ifor(int j=0; jout.print(this.mazePath[i][j].getValue()+" ");
}
System.out.println();
}
}
}
public class TestMazePathDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.print("请输入迷宫的行列数(m * n):");
int row = scan.nextInt();
int col = scan.nextInt();
//maze现在代表的就是整个迷宫
Maze maze = new Maze(row, col);
System.out.println("请输入迷宫的路径:");
for(int i=0; ifor(int j=0; j int data = scan.nextInt();
maze.setPath(i, j, data);
}
}
//以上代码,就把整个迷宫路径的信息都设置到maze对象里面了
//该函数主要用来更改迷宫节点四个方向的行走状态
maze.adjustMazePath();
//开始寻找迷宫路径
maze.findMazePath();
//打印最终的迷宫路径信息
maze.showMazePath();
}
}