poj 1915 bfs(STL的queue)
来源:互联网 发布:ios 网络图片高斯模糊 编辑:程序博客网 时间:2024/06/09 19:32
题意:给你个棋盘,然后给你初始点和终点,根据规则,求初始点跳到终点的最小步数。。
思路:广搜,每扩展一层,步数加一。。用queue写的~~
#include<iostream>#include<queue>#define MAX 305using namespace std;struct point{ int x,y; int step;};//到达的点,和需要的步数 int dx[8]={-2,-2,-1,-1,1,1,2,2};int dy[8]={1,-1,2,-2,2,-2,1,-1};bool vis[MAX][MAX];int sx,sy,ex,ey,tx,ty,L;int bfs(){ memset(vis,false,sizeof(vis));//初始化 queue<point> Q;//定义个队列 point start,node; start.x=sx;// start.y=sy;// start.step=0;//队列初始化 Q.push(start);//压进队列 int step,x,y; while(!Q.empty()) { start=Q.front();Q.pop();//取队列的头元素,同时把这个元素弹出 (队列从后往前进,先进的先出) x=start.x; y=start.y; step=start.step;//把队列头元素的x,y,step取出 if(x==ex&&y==ey) return step;//判断 for(int i=0;i<8;i++)//扩展 { tx=x+dx[i]; ty=y+dy[i]; if(tx>=0&&tx<L&&ty>=0&&ty<L&&!vis[tx][ty]) { node.x=tx; node.y=ty; node.step=step+1; Q.push(node);//满足条件的进队列 vis[tx][ty]=true; } } }}int main(){ int N; scanf("%d",&N); while(N--) { scanf("%d",&L); scanf("%d%d",&sx,&sy); scanf("%d%d",&ex,&ey); printf("%d\n",bfs()); }}
- poj 1915 bfs(STL的queue)
- POJ 3984 迷宫问题(BFS+STL queue)
- POJ - 3481 - Double Queue (STL)
- POJ 3481 Double Queue(STL)
- POJ 3481 Double Queue (STL SET)
- 广度优先搜索(BFS+STL queue)实现
- POJ 1915 Knight Moves(BFS+STL)
- POJ 3481 Double Queue(STL)
- POJ 3481 Double Queue(STL)
- POJ 3481 Double Queue(STL)
- poj 3481 Double Queue STL中map的运用
- POJ 3481 Double Queue(STL之双向优先队列)
- stl之queue(队列)的使用
- STL中队列(queue)的使用方法
- STL queue 的使用
- 替换STL的queue
- 替换STL的queue
- STL queue 的实现
- poj 1011 dfs
- poj 1190 dfs(生日蛋糕)
- VC调用CMD命令通过控制台管道获取输出结果
- poj 1915 第一个双向BFS
- hdu 1016 dfs(素数环)
- poj 1915 bfs(STL的queue)
- hdu 1495 bfs 非常可乐
- ie中不支持getElementsByName() 杯具
- MVC开发模式环境搭建
- hdu 1312 bfs
- hdu1258/poj1564 dfs
- hdu 1253 三维bfs
- poj 3984 BFS+DFS(保存路径)
- C++通过重载()运算符来仿造函数