链式队列实现迷宫寻径

测试类

public class TestQueue1 {
  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();
    }
    Migong4 mi=new Migong4(map);
    if(mi.findpath()){//寻找路径,存在路径时返回1
            System.out.println("迷宫有路,走迷宫的一条路径为:");
             //输出迷宫的路径
            Migong4.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 IQueue<E> {//定义队列接口
    boolean enqueue(E item); //入队列操作
    E dequeue(); //出队列操作
    E peek(); //取对头元素
    int size(); //求队列的长度
    boolean isEmpty(); //判断队列是否为空  
    boolean isFull(); //判断队列是否为满  
}

链式数据结构

public class QueueNode<E> {//链式结构
  private E data; // 数据域
  private QueueNode<E> next; // 引用域
  //构造函数
  public QueueNode(){}
  public QueueNode(E data) {
     this.data = data;
  }
  public QueueNode(E data, QueueNode<E> next) {
    this.data = data;
    this.next = next;
  }
  //数据域get属性
  public E getData() {
     return data;
  }
  //数据域set属性
  public void setData(E data) {
    this.data = data;
  }
  //引用域get属性
  public QueueNode<E> getNext() {
    return next;
  }
  //引用域get属性
  public void setNext(QueueNode<E> next) {
     this.next = next;
  }
}

单链表链实现队列数据结构

public class LinkQueue<E> implements IQueue<E> {//实现队列
  private QueueNode<E> front; // 队列头指示器
  private QueueNode<E> rear; // 队列尾指示器
  private int maxsize; // 队列的容量,假如为0,不限容量
  private int size; // 队列数据元素个数
  // 初始化链队列
  public LinkQueue() {
    front = rear = null;
    size = 0;
    maxsize = 0;
  }
  // 初始化限容量的链队列
  public LinkQueue(int maxsize) {
     super();
     this.maxsize = maxsize;
  }
  // 入队列操作
  public boolean enqueue(E item) {
      QueueNode<E> newnode = new QueueNode<E>(item);
      if (!isFull()) {
         if (isEmpty()) {
            front = newnode;
            rear = newnode;
         } else {
         rear.setNext(newnode);
         rear = newnode;
       }
       ++size;
      return true;
    }else
      return false;
  }
  // 出队列操作
  public E dequeue() {
   if (isEmpty())
      return null;
   QueueNode<E> node = front;
   front = front.getNext();
   if (front == null) {
       rear = null;
    }
    --size;
    return node.getData();
  }
  // 取对头元素
  public E peek() {
    if (!isEmpty()) {
       return front.getData();
    } else
      return null;
  }
  // 求队列的长度
  public int size() {
    return size;
  }
  // 判断队列是否为空
  public boolean isEmpty() {
     if ((front == rear) && (size == 0)) {
         return true;
     } else {
        return false;
     }
  }
  // 判断队列是否为满
  public boolean isFull() {
    if (maxsize != 0 && size == maxsize) {
       return true;
    } else {
       return false;
    }
  }
}

主方法

public class Migong4 {//链式队列存储结构实现迷宫
  int[][] maze;
  int row,col;
  LinkQueue< 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 Migong4(int[][] map){//构造方法
   row=map.length+2;
   col=map[0].length+2;
   sta= new LinkQueue<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.enqueue(temp);
   while(!sta.isEmpty()){
      temp=sta.dequeue();
      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.enqueue(temp);
             x=i;y=j;maze[x][y]=-1;
             if(x==row-2 && y==col-2){
                    temp=new Point1(x,y,-1);
                    sta.enqueue(temp);
                    return true;//迷宫可达
             }
             else d=0;//向下继续便利
          }
          else d++; 
       }
    }
    return false; 
  }
  public Point1[] getpath(){
     Point1[] points = new Point1[sta.size()];
     for(int k=points.length-1;k>=0;k--){
         points[k]=sta.dequeue();
     }
     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;
     }
  }

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include using std::cout; using std::cin; #include #include #include #define OVERFLOW -2 #define INIT_SIZE 100 //存储空间初始分配量 #define INCREMENT 10 //存储空间分配增量 typedef struct{ int r; int c; }PostType;//迷宫中r行c列的位置 typedef struct{ int ord; //当前位置在路径上的序号 PostType seat;//当前坐标 int di; //往下一坐标的方向 }SElemType; //栈元素类型 typedef struct{ SElemType* base;//栈基址,构造前销毁后为空 SElemType* top;//栈顶 int stackSize; //栈容量 }Stack; //栈类型 int InitStack(Stack &S){ //构造空栈s S.base=(SElemType*)malloc(INIT_SIZE *sizeof(SElemType)); if(!S.base) exit(OVERFLOW);//存储分配失败 S.top=S.base; S.stackSize=INIT_SIZE; return 0; } int StackEmpty(Stack S)//若s为空返回TRUE,否则返回FALSE { if(S.top==S.base) return 1; return 0; } int Push(Stack &S,SElemType e){ //插入元素e为新的栈顶元素 if(S.top-S.base >=S.stackSize){//栈满,加空间 S.base=(SElemType *)realloc(S.base,(S.stackSize+INCREMENT)*sizeof(SElemType)); if(!S.base) exit(OVERFLOW); //存储分配失败 S.top=S.base+S.stackSize; S.stackSize+=INCREMENT; } *S.top++=e; return 1; } int Pop(Stack &S,SElemType &e){//若栈不空删除栈,顶元素用e返回 if(S.top==S.base) return 0; e=*--S.top; return 1; } int DestroyStack(Stack &S){//销毁栈S, free(S.base); S.top=S.base; return 1; } #define MAXLEN 16//迷宫包括外墙最大行列数目 typedef struct{ int r; int c; char adr[MAXLEN][MAXLEN]; }MazeType; //迷宫类型 int InitMaze(MazeType &maze){ //初始化迷宫若成功返回TRUE,否则返回FALSE int i,j; cout<>maze.r>>maze.c; //迷宫行和列数 for(i=0;i<=maze.c+1;i++){//迷宫行外墙 maze.adr[0][i]='#'; maze.adr[maze.r+1][i]='#'; } for(i=0;i<=maze.r+1;i++){//迷宫列外墙 maze.adr[i][0]='#'; maze.adr[i][maze.c+1]='#'; } for(i=1;i<=maze.r;i++) for(j=1;j<=maze.c;j++) maze.adr[i][j]=' ';//初始化迷宫 int m=1,n=maze.c; for(m;m1;n--) maze.adr[6][n]='#'; maze.adr[4][5]='#'; maze.adr[4][6]='#'; maze.adr[3][3]='#'; maze.adr[3][2]='#'; maze.adr[5][7]='#'; maze.adr[5][6]='#'; maze.adr[8][3]='#'; maze.adr[7][2]='#'; maze.adr[7][5]='#'; return 1; }//InitMaze int Pass(MazeType maze,PostType curpos){ if(maze.adr[curpos.r][curpos.c]==' ') return 1; else return 0; }//Pass int FootPrint(MazeType &maze,PostType curpos){ //若走过并且可通返回TRUE,否则返回FALSE //在返回之前销毁栈S maze.adr[curpos.r][curpos.c]='!';//"*"表示可通 return 1; }//FootPrint PostType NextPos(PostType &curpos,int i){ //指示并返回下一位置的坐标 PostType cpos; cpos=curpos; switch(i){ case 1 : cpos.c+=1; break; case 2 : cpos.r+=1; break; case 3 : cpos.c-=1; break; case 4 : cpos.r-=1; break; default: exit(0); } return cpos; }//Nextpos int MarkPrint(MazeType &maze,PostType curpos){ maze.adr[curpos.r][curpos.c]='@';//"@"表示曾走过但不通 return 1; } int MazePath(MazeType &maze,PostType start,PostType end){ //若迷宫maze存在从入口start到end的通道则求得一条存放在栈中 Stack S; PostType curpos; int curstep;//当前序号,1.2.3.4分别表示东,南,西,北方向 SElemType e; InitStack(S); curpos=start; //设置"当前位置"为"入口位置" curstep=1; //探索第一步 do{ if(Pass(maze,curpos)){//当前位置可以通过,即是未曾走到过的通道 FootPrint(maze,curpos);//留下足迹 e.ord=curstep; e.seat=curpos; e.di=1; Push(S,e); //加入路径 if(curpos.r==end.r&& curpos.c==end.c) if(!DestroyStack(S))//销毁失败 exit(OVERFLOW); else return 1; //到达出口 else{ curpos=NextPos(curpos,1); //下一位置是当前位置的东邻 curstep++; //探索下一步 } } else{ //当前位置不通 if(!StackEmpty(S)){ Pop(S,e); while(e.di==4 && !StackEmpty(S)){ MarkPrint(maze,e.seat); Pop(S,e); //留下不能通过的标记,并退一步 } if(e.di < 4){ e.di++;//换下一个方向探索 Push(S,e); curpos=NextPos(e.seat,e.di);//设定当前位置是该新方向上的相邻位置 } } } }while(!StackEmpty(S)); if(!DestroyStack(S))//销毁失败 exit(OVERFLOW); else return 0; }//MazePath void PrintMaze(MazeType &maze){ //将标记路径信息的迷宫输出 int i,j; cout<<"\n——!为所求迷宫路线路线——:\n\n"; cout<<" "; for(i=0;i<=maze.r+1;i++)//打印列数名 cout<<" "<<i; cout<<"\n\n"; for(i=0;i<=maze.r+1;i++){ cout<<" "<<i;//打印行名 for(j=0;j<=maze.c+1;j++) cout<<" "<<maze.adr[i][j];//输出迷宫路径 cout<<"\n\n"; } } void main(){ MazeType maze; PostType start,end; char cmd; do{ cout<<"-------建立迷宫--------\n"; if(!InitMaze(maze)){ cout<<"\n——建立有误——!!!\n"; exit(OVERFLOW); } do{ cout<>start.r>>start.c; if(start.r>maze.r || start.c>maze.c){ cout<maze.r || start.c>maze.c); do{ cout<>end.r>>end.c; if(end.r>maze.r || end.c>maze.c){ cout<maze.r || end.c>maze.c); if(!MazePath(maze,start,end)) cout<<"\n不能求得路径!\n"; else PrintMaze(maze); cout<>cmd; }while(cmd=='y' || cmd=='Y'); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值