基于栈的数据结构实现走迷宫的最短路径(java)
语言:JAVA 开发平台:Eclipse
目的
一个迷宫如图所示,它有一个入口和出口,其中白色单元表示通路,黑色单元表示路不通。寻找一条从入口到出口的路径,每步只能从一个白色单元走到相邻的白色单元,直至出口。使用栈实现走迷宫的功能,演示走迷宫的过程。
数据结构设计
用ArrayList 作为栈的数据结。ArrayList是java中Collection集合中线性表,可以通过add.remove操作添加删除元素,coordition是定义的内部类,用来存放一个节点的横纵坐标。这样,栈中的每个元素就是迷宫中一个位置的坐标。
算法思路(流程)
寻路算法(若存在路径,返回true;若不存在,返回false):
while 栈非空
获取栈顶元素temp = stack.getTop
if temp是出口位置
跳出循环
else 继续向下执行
if temp上边可走 and temp上边未遍历
把temp所在位置标记为已遍历,将temp 上边的位置加入栈顶
进入下一次循环
if temp右边可走 and temp右边未遍历
把temp所在位置标记为已遍历,将temp 右边的位置加入栈顶
进入下一次循环
if temp下边可走 and temp下边未遍历
把temp所在位置标记为已遍历,将temp 下边的位置加入栈顶
进入下一次循环
if temp左边可走 and temp左边未遍历
把temp所在位置标记为已遍历,将temp 左边的位置加入栈顶
进入下一次循环
上下左右都不可走,标记temp为已读,并从栈中移出,进入下次循环
最后退出while循环有两种情况:
1) 找到出口位置,那栈中保留的就是从入口到出口的路径
2) 栈为空,说明没有路径能从入口到出口
实验结果分析
总结
栈是一种特殊的线性表,特殊之处在于插入和删除操作的位置受到限制,栈的插入和删除操作只允许在线性表的一端进行,特点是先进后出。允许操作的一端为栈顶,栈中插入元素的操作为入栈,删除元素的操作为出栈。
关键代码
package com.test;
import java.util.ArrayList;
import java.util.List;
public class Maprinth {
private int[][] map;//存放迷宫矩阵,0表示可走,1表示不可走
private coordinate start;//记录起点坐标
private coordinate end;//记录终点坐标
private List stack;//栈
/*
* 坐标
* */
class coordinate{
public int x;
public int y;
public coordinate(int x,int y){
this.x = x;
this.y = y;
}
}
/*
* 入栈
* **/
public void push(coordinate co){
if(this.stack!=null){
this.stack.add(co);
}
else
this.stack = new ArrayList<>();
stack.add(co);
}
/*
* 获取栈顶元素
* */
public coordinate getTop(){
if(!stack.isEmpty()){
return stack.get(stack.size()-1);
}
else
return null;
}
/*
* 出栈操作
* */
public coordinate pop(){
if(!stack.isEmpty())
return stack.remove(stack.size()-1);
else
return null;
}
/*
* 产生例题的迷宫矩阵
* */
public void generatemap2(){
map = new int[6][6];
map[0][0] = 0;map[0][1] = 1;map[0][2] = 0;map[0][3] = 0;map[0][4] = 0;map[0][5] = 1;
map[1][0] = 0;map[1][1] = 0;map[1][2] = 0;map[1][3] = 1;map[1][4] = 0;map[1][5] = 1;
map[2][0] = 1;map[2][1] = 0;map[2][2] = 1;map[2][3] = 0;map[2][4] = 0;map[2][5] = 1;
map[3][0] = 0;map[3][1] = 0;map[3][2] = 0;map[3][3] = 1;map[3][4] = 1;map[3][5] = 1;
map[4][0] = 0;map[4][1] = 1;map[4][2] = 1;map[4][3] = 0;map[4][4] = 0;map[4][5] = 0;
map[5][0] = 0;map[5][1] = 0;map[5][2] = 0;map[5][3] = 0;map[5][4] = 1;map[5][5] = 1;
start = new coordinate(0,0);
end = new coordinate(4,5);
//打印迷宫矩阵
printmap();
}
public void printmap(){
if(this.map!=null){
for(int i=0;i<map.length;i++){
for(int j=0;j<map.length;j++)
System.out.print(map[i][j]+" ");
System.out.println();
}
}
}
/*
* 寻路函数
* return true: 存在路径
* return false:不存在路径
* */
public boolean search(){
//记录遍历情况数组 0 表示未遍历 ,1 表示遍历
int[][] flag = new int[map.length][map.length];
//栈
this.stack = new ArrayList<>();
coordinate start = this.start;
coordinate end = this.end;
stack.add(start);
int i=0;
//栈非空
while((!stack.isEmpty()&&(i<10)){
//按照上 右 下 左的顺序遍历
//取栈顶
coordinate temp = getTop();
//终点则停止
if((temp.x == this.end.x)&&(temp.y==this.end.y))
break;
//向上
if(temp.x>0){
//上可走且未遍历
if((map[temp.x-1][temp.y]!=1)&&(flag[temp.x-1][temp.y]==0)){
//标记已遍历
flag[temp.x][temp.y] = 1;
//入栈
push(new coordinate(temp.x-1,temp.y));
continue;
}
}
//向右
if(temp.y<map.length-1){
//右可走且未遍历
if((map[temp.x][temp.y+1]!=1)&&(flag[temp.x][temp.y+1]==0)){
flag[temp.x][temp.y] = 1;
//stack.add(new coordinate(temp.x,temp.y+1));
//入栈
push(new coordinate(temp.x,temp.y+1));
continue;
}
}
//向下
if(temp.x<map.length-1){
//下可走且未遍历
if((map[temp.x+1][temp.y]!=1)&&(flag[temp.x+1][temp.y]==0)){
flag[temp.x][temp.y] = 1;
//stack.add(new coordinate(temp.x+1,temp.y));
//入栈
push(new coordinate(temp.x+1,temp.y));
continue;
}
}
//向左
if(temp.y>0){
//左可走且未遍历
if((map[temp.x][temp.y-1]!=1)&&(flag[temp.x][temp.y-1]==0)){
flag[temp.x][temp.y] = 1;
//stack.add(new coordinate(temp.x,temp.y-1));
//入栈
push(new coordinate(temp.x,temp.y-1));
continue;
}
}
//上下左右都不能走,标记已遍历并移出
flag[temp.x][temp.y] = 1;
//stack.remove(stack.size()-1);
//出栈
pop();
}
//出入口没有路径
if(stack.size()==0)
return false;
//找到路径
else
return true;
}
/*
* 打印路径
* **/
public void printPath(){
if(!stack.isEmpty()){
for(int i=0;i<stack.size()-1;i++){
System.out.print("("+stack.get(i).x+","+stack.get(i).y+")"+"->");
}
System.out.println("("+stack.get(stack.size()-1).x+","+stack.get(stack.size()-1).y+")");
}
}
public static void main(String[] args){
Maprinth Maprinth = new Maprinth();
Maprinth.generatemap2();
if(Maprinth.search())
//打印路径
Maprinth.printPath();
else
System.out.println("无可用路径!");
}