poj 1915-BFS
来源:互联网 发布:http端口号范围 编辑:程序博客网 时间:2024/06/03 19:24
http://poj.org/problem?id=1915
与2243题目大致意思一样,只是修改下范围,修改下上限与下限
程序如下:
#include <iostream>#include <stdio.h>#include <cstring>#include <queue>#define maxn 305using namespace std;int x1,y1,x2,y2,n;queue<int> xx;queue<int> yy;//定义一个队列int sum;int dx[8]={-2,-1,1,2,2,1,-1,-2};int dy[8]={-1,-2,-2,-1,1,2,2,1};bool map[maxn][maxn];int levels[maxn][maxn];bool inmap(int x,int y) //判断所走的路不要出边界{ return (x<n && y<n&&x>=0 &&y>=0);}void bfs(int x,int y)//广搜程序{ for(int k=0;k<8;k++) { if(inmap(x+dx[k],y+dy[k])&&!map[x+dx[k]][y+dy[k]]) //在有界的范围内,且没有被访问过 { map[x+dx[k]][y+dy[k]]=true; levels[x+dx[k]][y+dy[k]]=levels[x][y]+1; xx.push(x+dx[k]); yy.push(y+dy[k]);//让其入栈 if((x+dx[k]==x2)&&(y+dy[k]==y2)) //直到等于x2,y2 { sum=levels[x+dx[k]][y+dy[k]];//此时的和为sum return; } } } if(!xx.empty()) //出栈 { xx.pop(); yy.pop(); } if(!xx.empty()) { bfs(xx.front(),yy.front());//取队首 }}int main(){ int cas; scanf("%d",&cas); while(cas--) { scanf("%d",&n); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); sum=0; while(!xx.empty()) { xx.pop(); yy.pop(); } memset(map,false,sizeof(map)); memset(levels,0,sizeof(levels)); xx.push(x1);// yy.push(y1); map[x1][y1]=true; levels[x1][y1]=0;//初始化最初的点 bfs(x1,y1); printf("%d\n",sum); } return 0;}
0 0
- poj 1915-BFS
- poj~1915(bfs)
- POJ 1915 BFS题目
- poj 1915 BFS
- POJ 1915 Knight Moves bfs || 双向bfs
- poj 1915 bfs(骑士移动)
- POJ 1915 Knight Moves bfs
- POJ 1915Knight Moves(BFS)
- poj 1915 Knight Moves(bfs)
- poj 1915 bfs 模板化
- poj 1915 Knight Moves(bfs)
- POJ 1915 BFS 同HDU1372
- POJ 1915 BFS-Knight Moves
- [BFS]poj 1915 Knight Moves
- Poj 1915 Knight Moves ( BFS
- POJ 1915 Knight Moves BFS
- 【BFS】poj 1915 Knight Moves
- [BFS] poj
- 桶排序
- HDOJ 题目1690Bus System(最短路径)
- iOS开发中常用的代码
- 在COCOS2D-X中实现橡皮擦功能
- eclipse 出现 !MESSAGE An error occurred while automatically activating bundle
- poj 1915-BFS
- 数学之美系列十三:信息指纹及其应用
- 你还在为如何写CSS代码白了少年头嘛??
- 非常实用的Android模拟器CMD命令
- 行业应用之手机移动办公
- 扩展欧几里得及其应用
- Memcache的最佳实践方案
- Linux修改进程名称(setproctitle())
- HDOJ 题目4556Stern-Brocot Tree(数学,欧拉函数)