poj 1915 Knight Moves
来源:互联网 发布:女生学编程好吗 编辑:程序博客网 时间:2024/05/19 04:07
poj 1915 Knight Moves 题目链接:http://poj.org/problem?id=1915
题目大意:象棋里马走日,此题中给出棋盘大小,给出马的起止位置,问马跳过去最少要跳多少步。
题目分析:简单BFS,与以前稍有不同之处在dir数组。
code:
#include<cstdio>#include<cstring>#include<queue>using namespace std;int n,dir[8][2]={1,2,-1,2,2,1,2,-1,1,-2,-2,-1,-1,-2,-2,1};char g[510][510];struct node{int x,y,step;};bool judge(int x,int y){return x<n&&y<n&&x>=0&&y>=0&&g[x][y]==0;}int bfs(int x,int y,int ex,int ey){queue<node>q;node first,next;first.x=x,first.y=y,first.step=0;q.push(first);while(!q.empty()){first=q.front();if(first.x==ex&&first.y==ey)return first.step;q.pop();g[first.x][first.y]=1;for(int i=0;i<8;i++){next.x=first.x+dir[i][0];next.y=first.y+dir[i][1];if(judge(next.x,next.y)){next.step=first.step+1;q.push(next);if(next.x==ex&&next.y==ey)return next.step;g[next.x][next.y]=1;}}}return -1;//*}int main(){int i,j,t,sx,sy,ex,ey;scanf("%d",&t);while(t--){memset(g,0,sizeof(g));scanf("%d%d%d%d%d",&n,&sx,&sy,&ex,&ey);printf("%d\n",bfs(sx,sy,ex,ey));}return 0;}PS:好惨啊,又被虐了……这次是怎么回事呢?问题出在注释星号的那里,一开始那一行没写,本来以为不可能走到那里,所以不用控制那种情况,从discuss里找到数据后发现一个极尽坑跌的情况,说好的(4 <= l <= 300)呢!?l居然可能是2,不加控制的结果就是随机数,然后就华丽的错了。那一行返回的值写0也可以,亲测。
0 0
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- POJ 1915 Knight Moves
- poj 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915Knight moves
- POJ 1915 Knight Moves
- POJ 1915Knight Moves
- POJ 1915: Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- android jni socket
- Java阻塞队列SynchronousQueue实现并发
- 协议: DTMF相关FAQ
- 我的android程序员面试经历(一)(北京)
- MPI(Message Passing Interface)
- poj 1915 Knight Moves
- android面试题总结加强版(二)
- 解决Weblogic域创建、启动、进入控制台慢问题
- mysql远程连接 windows配置
- android面试题总结加强版(一)
- hadoop2.2.0源码编译(ubuntu下)
- ios:给appstore评分和进入app安装页面代码
- 几种华丽无比的折磨人的开发方式
- Light OJ 1422 - Halloween Costumes (区间DP)