439 - Knight Moves

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int cost[100][100],color[9][9];
int X[8]= {2,1,-1,-2,-2,-1,1,2};
int Y[8]= {1,2,2,1,-1,-2,-2,-1};
int main()
{
    char a,b;
    int c,d,e,f;
    while(scanf("%c%d",&a,&c)==2)
    {
        getchar();
        scanf("%c%d",&b,&d);
        getchar();
        if(a=='a')
        {
            e=1;
        }
        else if(a=='b')
        {
            e=2;
        }
        else if(a=='c')
        {
            e=3;
        }
        else if(a=='d')
        {
            e=4;
        }
        else if(a=='e')
        {
            e=5;
        }
        else if(a=='f')
        {
            e=6;
        }
        else if(a=='g')
        {
            e=7;
        }
        else if(a=='h')
        {
            e=8;
        }
        if(b=='a')
        {
            f=1;
        }
        else if(b=='b')
        {
            f=2;
        }
        else if(b=='c')
        {
            f=3;
        }
        else if(b=='d')
        {
            f=4;
        }
        else if(b=='e')
        {
            f=5;
        }
        else if(b=='f')
        {
            f=6;
        }
        else if(b=='g')
        {
            f=7;
        }
        else if(b=='h')
        {
            f=8;
        }
        int ux,uy,vx,vy,k;

        queue<int>Q;

        Q.push(e);
        Q.push(c);
        color[e][c]=1;
        while(!Q.empty())
        {
            ux=Q.front();
            Q.pop();
            uy=Q.front();
            Q.pop();
            for(k=0; k<8; k++)
            {
                vx=ux+X[k];
                vy=uy+Y[k];
                if((vx>=1&&vx<=8)&&(vy>=1&&vy<=8)&&!color[vx][vy])
                {
                    color[vx][vy]=1;
                    Q.push(vx);
                    Q.push(vy);
                    cost[vx][vy]=cost[ux][uy]+1;
                }
            }
        }
        printf("To get from %c%d to %c%d takes %d knight moves.\n",a,c,b,d,cost[f][d]);
        memset(cost,0,sizeof(cost));
        memset(color,0,sizeof(color));
    }
    return 0;
}

0 comments: (+add yours?)