poj1915 BFS
来源:互联网 发布:怎么用u盘安装mac系统 编辑:程序博客网 时间:2024/05/22 15:10
/** * poj1915 BFS * 乍一看以为是DP,想了半天也没有想出一个特别好的dp方案来,果然还是走BFS吧 * 做法比较简单粗暴了,代码也很粗暴,就是宽搜,从最初始点开始,8种方向只要能走就走一步,只要这个点还没有走过,就将其作为新的出现的点记录下来 * 依照步数从小往大升序地遍历,得到的一定是最小值 */#include <cstdio>#include <cstring>const int MAX_NUM = 301;struct point{ int x; int y; int step;} p[MAX_NUM*MAX_NUM];bool flag[MAX_NUM][MAX_NUM];int l;int father,child;int xstart,xend,ystart,yend;void bfs(){ father = 0; child = 1; p[0].x = xstart; p[0].y = ystart; p[0].step = 0; flag[xstart][ystart] = true; int x,y,step; if(xstart == xend && ystart == yend){ printf("0\n"); return; } while(1){ x = p[father].x; y = p[father].y; step = p[father].step; //x-2 y-1 if(x>=2 && y>=1){ if(!flag[x-2][y-1]){ flag[x-2][y-1] = true; p[child].x = x-2; p[child].y = y-1; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } //x-2 y+1 if(x>=2 && y<l-1){ if(!flag[x-2][y+1]){ flag[x-2][y+1] = true; p[child].x = x-2; p[child].y = y+1; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } //x-1 y+2 if(x>=1 && y<l-2){ if(!flag[x-1][y+2]){ flag[x-1][y+2] = true; p[child].x = x-1; p[child].y = y+2; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } //x-1 y-2 if(x>=1 && y>=2){ if(!flag[x-1][y-2]){ flag[x-1][y-2] = true; p[child].x = x-1; p[child].y = y-2; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } //x+1 y-2 if(x<l-1 && y>=2){ if(!flag[x+1][y-2]){ flag[x+1][y-2] = true; p[child].x = x+1; p[child].y = y-2; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } //x+1 y+2 if(x<l-1 && y<l-2){ if(!flag[x+1][y+2]){ flag[x+1][y+2] = true; p[child].x = x+1; p[child].y = y+2; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } //x+2 y-1 if(x<l-2 && y>=1){ if(!flag[x+2][y-1]){ flag[x+2][y-1] = true; p[child].x = x+2; p[child].y = y-1; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } //x+2 y+1 if(x<l-2 && y<l-1){ if(!flag[x+2][y+1]){ flag[x+2][y+1] = true; p[child].x = x+2; p[child].y = y+1; p[child].step = step+1; if(p[child].x == xend && p[child].y == yend){ break; } ++child; } } ++father; } printf("%d\n",p[child].step);}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&l); scanf("%d%d%d%d",&xstart,&ystart,&xend,¥d); for(int i=0;i<l;++i){ memset(flag[i],0,sizeof(flag[i])); } bfs(); } return 0;}
0 0
- poj1915 BFS
- poj1915 bfs
- POJ1915 BFS
- POJ1915 BFS&双向BFS
- POJ1915 经典bfs
- (未)poj1915(bfs)
- POJ1915:Knight Moves(BFS)
- poj1915 简单bfs
- POJ1915 (Knight Moves) BFS
- poj1915之BFS
- poj1915~双向bfs和bfs
- POJ1915 Knight Moves 经典BFS
- BFS广度优先搜索 poj1915
- poj1915 Knight Moves(bfs)
- poj1915 Knight Moves(bfs)
- poj1915--Knight Moves(初级BFS)
- POJ1915(Knight Moves,双向BFS)
- POJ1915
- i386中的状态和控制寄存器
- nodejs总结
- html5本地存储的兼容性写法
- poj1745 0-1DP
- poj1830 高斯消元法
- poj1915 BFS
- 无向图最小生成树、次小生成树、最短路径模版
- 开源 免费 java CMS - FreeCMS1.7 建站向导
- spring IOC 实现原理模拟实现
- Ubuntu启动问题以及Grub Rescue修复方法
- poj1950 DFS
- web在线显示3d立体汉字
- WinForm中,将DataGridView中的某一行某一列设置为下拉列表时候单独绑定数据源
- 风