POJ_1915_KnightMoves
2015-07-25 08:55
351 查看
bfs
就是跟马的运动方式一样么……
就是跟马的运动方式一样么……
#include <iostream> #include <stdio.h> #include <queue> using namespace std; const int M=305; int isu[M][M]; int dx[8]={-2,-1,1,2,2,1,-1,-2}; int dy[8]={-1,-2,-2,-1,1,2,2,1}; queue<int>re; int tot; void bfs(int sx,int sy,int ex,int ey,int n) { while(!re.empty()) re.pop(); re.push(sx*n+sy); isu[sx][sy]=1; while(!re.empty()) { int t=re.front(); re.pop(); int cx=t/n,cy=t%n; //cout<<tot<<" "<<cx<<" "<<cy<<endl; for(int i=0;i<8;i++) { int x=cx+dx[i],y=cy+dy[i]; if(0<=x&&x<n&&y>=0&&y<n&&!isu[x][y]) { isu[x][y]=isu[cx][cy]+1; re.push(x*n+y); if(x==ex&&y==ey) { tot=isu[x][y]; return; } } } } } void clear() { for (int i = 0; i < M;i++) for (int j = 0; j < M; j++) isu[i][j] = 0; } int main() { int t; int n,xj,yj,xm,ym; scanf("%d",&t); while (t--) { tot=0; scanf("%d%d%d%d%d", &n,&xj,&yj,&xm,&ym); if(xj==xm&&yj==ym) { printf("0\n"); continue; } clear(); bfs(xj,yj,xm,ym,n); printf("%d\n", tot-1); //因为不算起点 } return 0; }
相关文章推荐
- AWK学习笔记
- 看看C# 6.0中那些语法糖都干了些什么(中篇)
- 骑士的移动
- 看看C# 6.0中那些语法糖都干了些什么(上篇)
- 什么是Spark?
- OpengGL绘制立方体的三种方法
- 【黑马程序员】----java基础---反射
- 【HTML学习】初识HTML5
- OC 中常用的占位符
- HDU_1312_ZOJ_2165_RedAndBlack
- MYSQL---多表删除
- G - Arctic Network - poj2349
- Java学习之路:详细解释Java解析XML四种方法
- oracle 存储过程访问其他用户的表
- android各种组件的监听器
- shell-scripts,FTP自动化上传脚本,可循环,直到上传成功为止
- HDU5291DPn堆糖果取出相等数目的糖果
- 南阳oj&nbsp;&nbsp;语言入门&nbsp;…
- 南阳oj&nbsp;&nbsp;语言入门&nbsp;计算球的体积
- 南阳oj&nbsp;&nbsp;语言入门&nbsp;…