顺序栈实现迷宫寻径

测试类

public class MazeTest{
  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();
        }
        Migong mi=new Migong(map);
        if(mi.findpath()){//寻找路径,存在路径时返回1
            System.out.println("迷宫有路,走迷宫的一条路径为:");
             //输出迷宫的路径
            test2.Migong.Point[] 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 IStack<E> {
 E push(E item); //入栈
 E pop(); //出栈
 E peek(); //取栈顶元素
 int size(); //返回栈中元素的个数
 boolean empty(); //判断栈是否为空
}
数组实现

public class SeqStack<E> implements IStack<E>{//顺序表
  private int maxsize; // 顺序栈的容量
  private E[] data; // 数组,用于存储顺序栈中的数据元素
  private int top; // 指示顺序栈的栈顶
  // 初始化栈
  public SeqStack(Class<E> type, int size) {
    E[] newInstance = (E[]) Array.newInstance(type, size);
    data = newInstance;
    maxsize = size;
    top = -1;
 }
 // 入栈操作
 public E push(E item) {
   if (!isFull()) {
     data[++top] = item;
     return item;
   }
   else
     return null;
 }
 // 出栈操作
 public E pop() {
   E item = null;
   if (!empty()) {
       item = data[top--];
   }
   return item;
 }
 // 获取栈顶数据元素
 public E peek() {
   E item = null;
   if (!empty()) {
     item = data[top];
   }
   return item;
 }
 //求栈的长度
 public int size() {
   return top+1;
 }
 // 判断顺序栈是否为空
 public boolean empty() {
    if (top == -1) {
      return true;
    } else {
      return false;
    }
}
// 判断顺序栈是否为满
public boolean isFull() {
  if (top == maxsize - 1) {
    return true;
  } else {
     return false;
  }
 }
}

主方法类

public class Migong {
   int[][] maze;
   int row,col;
   SeqStack< Point> stack;
   Point[] move={new Point(0,1),new Point(1,1),//八个方向
    new Point(1,0),new Point(1,-1),new Point(0,-1),
    new Point(-1,1),new Point(-1,0),new Point(-1,1)};
  public Migong(int[][] map){//构造方法
    row=map.length+2;
    col=map[0].length+2;
    stack= new SeqStack<Point>(Point.class,row*col);//创建栈 使用链式栈,也可以使用顺序栈
    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;
    Point temp=null;
    temp=new Point(1,1,-1);
    stack.push(temp);
    while(!stack.empty()){
       temp=stack.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 Point(x,y,d);
              stack.push(temp);
              x=i;y=j;maze[x][y]=-1;
              if(x==row-2 && y==col-2){
                       temp=new Point(x,y,-1);
                       stack.push(temp);
                       return true;//迷宫可达
               }
               else d=0;//向下继续便利
           }
           else d++; // 
       }
    }
    return false; 
  }
  public Point[] getpath(){
    Point[] points = new Point[stack.size()];
    for(int k=points.length-1;k>=0;k--){
      points[k]=stack.pop();
    }
    return points;
  }  
  public class Point{
    public int x,y,d;
    public Point(int x,int y){
      this.x=x;
      this.y=y;
    }
    public Point(int x,int y,int d){
      this.x=x;
      this.y=y;
      this.d=d;
   }
  }
}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值