首页 > 编程语言 > 详细

迷宫问题的广度优先算法

时间:2016-03-14 18:39:44      阅读:215      评论:0      收藏:0      [点我收藏+]
#include using namespace std; struct node { int x; int y; int f; //f父亲在队列中的编号, 输出路径用 int step; }; int main() { struct node que[2501]; int a[51][51]={0}; int book[51][51]={0}; int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int head,tail; int i,j,k,n,m,startx,starty,p,q,tx,ty,flag; cout<<"Please input the bianjie:"; cin>>n>>m; cout<<"Please input the array:"; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>a[i][j]; cout<<"please input the startx, starty, p,q:"; cin>>startx>>starty>>p>>q; //队列初始化 head=1; tail=1; //往队列插入迷宫入口坐标 que[tail].x=startx; que[tail].y=starty; que[tail].f=0; que[tail].step=0; tail++; book[startx][starty]=1; flag=0; while(headn||ty<1||ty>m) continue; if(a[tx][ty]==0&&book[tx][ty]==0) { book[tx][ty]=1; que[tail].x=tx; que[tail].y=ty; que[tail].f=head; que[tail].step=que[head].step+1; tail++; } if(tx==p&&ty==q) { flag=1; break; } } if(flag==1) break; head++; } cout<迷宫问题的广度优先算法

原文:http://www.cnblogs.com/wujixing/p/5276561.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!