| ||||||||||
| 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<iostream>
#include<string>
using namespace std;
char s1[3],s2[3];
int res1,res2;
int dir1[8]={-2,-1,1,2,2,1,-1,-2};
int dir2[8]={1,2,2,1,-1,-2,-2,-1};
struct node
{
int x,y;
int v;
};
bool having[9][9]={0};
struct node all[100];
int i=0,j=1;
void ouput(int a)
{
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,a);
}
bool bfs()
{
int a=all[i].x,b=all[i].y;
//右上方1
int k;
for(k=0;k<8;k++)
{
if( a+dir1[k]>=1 && a+dir1[k]<=8
&& b+dir2[k]>=1 && b+dir2[k]<=8
&& having[ a+dir1[k] ][ b+dir2[k] ]==false
)
{
if( a+dir1[k]==res1 && b+dir2[k]==res2 )
{
ouput( all[i].v +1 );
return true;
}
struct node n;
n.x=a+dir1[k];
n.y=b+dir2[k];
n.v=all[i].v+1;
all[j++]=n;
having[n.x][n.y]=true;
}
}
i++;
return false;
}
int main()
{
while( scanf("%s%s",s1,s2)!=EOF )
{
i=0; j=1;
memset(all,0,sizeof(all));
memset(having,0,sizeof(having));
struct node n;
n.x=s1[0]-'a'+1;
n.y=s1[1]-'1'+1;
n.v=0;
all[0]=n;
res1=s2[0]-'a'+1;
res2=s2[1]-'1'+1;
if( strcmp(s1,s2)==0)
{
ouput(0);
continue;
}
while(bfs()==false);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator