| ||||||||||
| 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 | |||||||||
bfs副代码#include <stdio.h>
#include <string.h>
#define N 9
int maze[N][N];
int sx,sy,ex,ey;
int head,tail;
typedef struct
{
int x,y,time;
}point;
point p[1024];
int d[8][2]={{2,1},{2,-1},{-2,-1},{-2,1},{1,2},{1,-2},{-1,-2},{-1,2}};
int times;
void bfs()
{
int i,tempx,tempy;
head=0;
tail=0;
p[head].x=sx;
p[head].y=sy;
p[head++].time=0;
while(tail<=head)
{
if(p[tail].x==ex&&p[tail].y==ey)
{
times=p[tail].time;
return;
}
else
{
for(i=0;i<8;i++)
{
tempx=p[tail].x+d[i][0];
tempy=p[tail].y+d[i][1];
if(tempx>=1&&tempy<=8&&tempy>=1&&tempy<=8&&!maze[tempx][tempy])
{
maze[tempx][tempy]=1;
p[head].x=tempx;
p[head].y=tempy;
p[head++].time=p[tail].time+1;
}
}
}
tail++;
}
}
int main()
{
char e1,e2;
int s1,s2;
while(scanf("%c%d %c%d",&e1,&s1,&e2,&s2)!=EOF)
{
getchar();
memset(maze,0,sizeof(maze));
sx=s1;
sy=e1-'a'+1;
ex=s2;
ey=e2-'a'+1;
maze[sx][sy]=1;
bfs();
printf("To get from %c%d to %c%d takes %d knight moves.\n",e1,s1,e2,s2,times);
}
return 1;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator