| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
哪位大神来救救我啊!~我快受不了了。测试这么多数据都没找出问题。我快不行了,指点下吧!~#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
struct node
{
int x,y,step;
};
int go[8][2]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
node n,now,s;
queue<node> qu;
int sx,sy,ex,ey;
int visit[9][9];
char A,C,E;
int b,d;
bool judge(int a,int b)
{
if(a>0&&a<=8&&b>0&&b<=8)
return true;
else
return false;
}
void bfs()
{
int i;
n.x=sx;
n.y=sy;
n.step=0;
visit[n.x][n.y]=1;
qu.push(n);
while(!qu.empty()){
s=qu.front();
qu.pop();
for(i=0;i<8;i++){
now.x=s.x+go[i][0];
now.y=s.y+go[i][1];
if(judge(now.x,now.y)&&!visit[now.x][now.y]){
now.step=s.step+1;
visit[now.x][now.y]=1;
qu.push(now);
if(now.x==ex&&now.y==ey){
printf("To get from %c%d to %c%d takes %d knight moves.\n",A,b,C,d,now.step);
return;
}
}
}
}
}
int main(void)
{
while(scanf("%c%d%c%c%d",&A,&b,&E,&C,&d)!=EOF){
getchar();
while(!qu.empty()){
qu.pop();
}
sx=A-96;
sy=b;
ex=C-96;
ey=d;
if(sx==ex&&ex==ey)
printf("To get from %c%d to %c%d takes 0 knight moves.\n",A,b,C,d);
else{
memset(visit,0,sizeof(visit));
bfs();
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator