测试类
public class maintest {
public static void main(String[] args) {//主方法类
int[][] map={
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,1,0,1,1,1,1},
{1,0,0,1,0,1,0,1,1,1},
{1,1,1,1,0,1,0,0,1,1},
{1,0,1,0,1,1,1,0,1,1},
{1,1,0,1,1,1,0,0,0,1},
{1,0,1,1,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int row=map.length,col=map[0].length;
System.out.println("迷宫矩阵:");
for(int i=1;i<row-1;i++){
for(int j=1;j<col-1;j++){
System.out.print(map[i][j]+" ");
}
System.out.println();
}
Migong2 mi=new Migong2(map);
if(mi.findpath()){//寻找路径,存在路径时返回1
System.out.println("迷宫有路,走迷宫的一条路径为:");
//输出迷宫的路径
Migong2.Point1[] points=mi.getpath();
for(int o=0;o<points.length;o++){
System.out.print("("+points[o].x+","+points[o].y+")");
}
}else System.out.println("迷宫无路!");
}
}
实现栈
栈接口
public interface Stack<E>{
E push(E item); //入栈
E pop(); //出栈
E peek(); //取栈顶元素
int size(); //返回栈中元素的个数
boolean empty(); //判断栈是否为空
}
链式数据类型
public class StackNode<E> {//定义对数据设置获取等基本操作,简化对对象的操作步骤,链表的结构所以从链头加入
private E data; // 数据域
private StackNode<E> next; // 引用域
//构造函数
public StackNode(){}
public StackNode(E data) {
this.data = data;
}
public StackNode(E data, StackNode<E> next) {
super();
this.data = data;
this.next = next;
}
//数据域get属性
public E getData() {
return data;
}
//数据域set属性
public void setData(E data) {
this.data = data;
}
//引用域get属性
public StackNode<E> getNext() {
return next;
}
//引用域get属性
public void setNext(StackNode<E> next) {
this.next = next;
}
}
实现链式栈
public class LinkStack<E> implements Stack<E> {
private StackNode<E> top; // 栈顶指示器
private int size; // 栈中结点的个数
// 初始化链栈
public LinkStack() {
top = null;
size = 0;
}
// 入栈操作
public E push(E item) {
StackNode<E> newnode = new StackNode<E>(item);
if (!empty())
newnode.setNext(top);
top = newnode;
++size;
return item;
}
// 出栈操作
public E pop() {
E item=null;
if (!empty()){
item = top.getData();
top = top.getNext();
size--;
}
return item;
}
// 获取栈顶数据元素
public E peek() {
E item=null;
if (!empty()){
item=top.getData();
}
return item;
}
// 求栈的长度
public int size() {
return size;
}
// 判断顺序栈是否为空
public boolean empty() {
if ((top == null) && (size == 0)){
return true;
}else{
return false;
}
}
}
主方法类
public class Migong2 {//寻找迷宫的方法类
int[][] maze;
int row,col;
LinkStack< Point1> sta;
Point1[] move={new Point1(0,1),new Point1(1,1),//八个方向
new Point1(1,0),new Point1(1,-1),new Point1(0,-1),
new Point1(-1,1),new Point1(-1,0),new Point1(-1,1)};
public Migong2(int[][] map){//构造方法
row=map.length+2;
col=map[0].length+2;
sta= new LinkStack<Point1>();//创建栈 使用链式栈,也可以使用顺序栈
maze=new int[row][col];
for(int X=1;X<row-1;X++){
for(int Y=1;Y<col-1;Y++){
maze[X][Y]=map[X-1][Y-1];
}
}
}
public boolean findpath(){//探寻路径
row=maze.length;
col=maze[0].length;
int x,y,d,i,j;
Point1 temp=null;
temp=new Point1(1,1,-1);
sta.push(temp);
while(!sta.empty()){
temp=sta.pop();
x=temp.x;y=temp.y;d=temp.d+1;
while(d<8){//广度优先搜索
i=x+move[d].x; j=y+move[d].y;
if(maze[i][j]==1){
temp=new Point1(x,y,d);
sta.push(temp);
x=i;y=j;maze[x][y]=-1;
if(x==row-2 && y==col-2){
temp=new Point1(x,y,-1);
sta.push(temp);
return true;//迷宫可达
}
else d=0;//向下继续便利
}
else d++; //
}
}
return false;
}
public Point1[] getpath(){//将栈的数据存进point数组里面
Point1[] points = new Point1[sta.size()];
for(int k=points.length-1;k>=0;k--){
points[k]=sta.pop();
}
return points;
}
public class Point1{//用于存储结点
public int x,y,d;
public Point1(int x,int y){
this.x=x;
this.y=y;
}
public Point1(int x,int y,int d){
this.x=x;
this.y=y;
this.d=d;
}
}
}