bfs模版
2015-08-22 16:12
183 查看
//用队列 #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; int x1,x2,y2,x3,y3,y1; int dir[8][2]= {0,1,0,-1,-1,0,1,0,1,1,1,-1,-1,1,-1,-1}; int mark[10][10]; struct nodes { int x,y,m; }; nodes node1,node2; void bfs() { queue<nodes> q; while(!q.empty()) q.pop(); node1.x=x1; node1.y=y1; node1.m=0; mark[x1][y1]=1; q.push(node1); while(!q.empty()) { node1=q.front(); q.pop(); if(node1.x==x2&&node1.y==y2) printf("%d\n",node1.m); for(int i=0; i<8; i++) { node2.x=node1.x+dir[i][0]; node2.y=node1.y+dir[i][1]; if(node2.x>=1&&node2.x<=8&&node2.y>=1&&node2.y<=8&&!mark[node2.x][node2.y]) { mark[node2.x][node2.y]=1; node2.m=node1.m+1; q.push(node2); } } } } int main() { int k=0; while(~scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3)) { k++; memset(mark,0,sizeof(mark)); mark[x3][y3]=1; printf("Case %d: ",k); bfs(); } }
相关文章推荐
- OCiOS开发:音频播放器 AVAudioPlayer
- UVa:1610 Party Games(字符串处理)
- C++builder的文件读写操作总结(3)
- Java进阶06 容器
- 揭开私有继承的面纱
- 线段树维护区间的和和单点更新-HDU1166-敌兵布阵
- hdu 1698 Just a Hook(线段树成段更新+延迟标记).
- [python] 命令行参数
- CF268C. Beautiful Sets of Points
- 浏览器的重绘、回流及网页优化
- HDU1556Color the ball
- jquery+css实现的红色线条横向二级菜单效果
- The one who can give anything you want is only yourself. The rating is the only you should pursue.
- CODE COMPLETE 2e Chapter 7
- Jenkins 的安装与简单使用
- Python TCP服务器
- 2014——我的求职(两)
- PPT是什么
- oc图片压缩与Base46编码
- pat----打印沙漏