杭电1372

bfs水题

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int d[8][2]={-1,-2,-2,-1,1,-2,2,-1,-2,1,-1,2,1,2,2,1};
struct node {
    char x;
    int y,t;
}a,b;
int vis[200][10];
int vj(char x,int y){
    if(x>='a'&&x<='h'&&y>0&&y<=8&&!vis[x][y])return 1;
    return 0;
}
int main(){
    char x,x1;
    int y,y1;
    while(~scanf("%c",&x1)){
        scanf("%d",&y1);
        a.x=x1;
        a.y=y1;
        a.t=0;
        getchar();
        scanf("%c%d",&x,&y);
        getchar();
        memset(vis,0,sizeof(vis));
        queue<node>k;
        k.push(a);
        vis[a.x][a.y]=1;
        while(!k.empty()){
            a=k.front();
            k.pop();
            if(a.x==x&&a.y==y){
                printf("To get from %c%d to %c%d takes %d knight moves.\n",x1,y1,x,y,a.t);
                break;
            }
            for(int i=0;i<8;i++){
                char xx=a.x+d[i][0];
                int yy=a.y+d[i][1];
                if(vj(xx,yy)){
                    b.x=xx;
                    b.y=yy;
                    b.t=a.t+1;
                    vis[b.x][b.y]=1;
                    k.push(b);
                }
            }
        }
    }
    return 0;
}


你可能感兴趣的:(杭电1372)