-
//zoj1091 Knight Moves
-
//Accepted 1091 C ++ 00:00.04 396K
-
#include <stdio.h>
-
#include <string.h>
-
char sb[3],se[3];
-
const int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
-
int inside(int x,int y)
-
{
-
if (x>=0 && x<8 && y>=0 && y<8) return 1;
-
return 0;
-
}
-
void solve()
-
{
-
int cnt[8][8];
-
int step = 0;
-
int by=sb[0]-'a',bx=sb[1]-'1',ey=se[0]-'a',ex=se[1]-'1';
-
memset(cnt,-1,sizeof(cnt));
-
cnt[bx][by] = 0;
-
while (cnt[ex][ey]==-1){
-
for (int x=0; x<8; ++x)
-
for (int y=0; y<8; ++y)
-
if (cnt[x][y]==step)
-
for (int i=0; i<8; ++i){
-
int tx=x+dir[i][0],ty=y+dir[i][1];
-
if (inside(tx,ty)) cnt[tx][ty] = step+1;
-
}
-
step++;
-
}
-
printf ("To get from %s to %s takes %d knight moves./n",sb,se,step);
-
-
}
-
int main()
-
{
-
#ifdef ONLINE_JUDGE
-
#else
-
freopen("1091.txt","r",stdin);
-
#endif
-
while (scanf("%s %s",&sb,&se)!=EOF)
-
solve();
-
#ifdef ONLINE_JUDGE
-
#else
-
fclose(stdin);
-
#endif
-
return 0;
-
}
zoj1091 Knight Moves
最新推荐文章于 2017-08-17 07:01:18 发布