HDU 1372 Knight Moves

以前敲过pascal的,一直感觉很麻烦……

#include<iostream>  

#include<queue>  

using namespace std;  

struct node  

{  

    int c,r,lev;  

}front,tmp,start,end;  

queue <node>Q;  

int a[]={0,-2,-2,-1,-1,1,1,2,2};  

int b[]={0,-1,1,-2,2,-2,2,-1,1};  

int bfs(node s,node e)  

{  

    int i;  

    while(!Q.empty())  

    Q.pop();  

    if(e.c==s.c&&e.r==s.r)return 0;  

    Q.push(s);  

    while(!Q.empty())  

    {  

        front=Q.front();  

        Q.pop();  

        for(i=1;i<=8;i++)  

        {  

            tmp.c=b[i]+front.c;  

            tmp.r=a[i]+front.r;  

            tmp.lev=front.lev+1;  

            if(e.c==tmp.c&&e.r==tmp.r)  return tmp.lev;  

            if(tmp.c>=1&&tmp.c<=8&&tmp.r>=1&&tmp.r<=8)  

            Q.push(tmp);  

        }  

     }  

}  

int main()  

{  

    char s[3],e[3];  

    while(scanf("%s%s",s,e)!=EOF)  

    {  

        start.c=s[0]-'a'+1;  

        start.r=s[1]-'0';  

        start.lev=0;  

        end.c=e[0]-'a'+1;  

        end.r=e[1]-'0';  

        printf("To get from %s to %s takes %d knight moves.\n",s,e,bfs(start,end));  

    }  

    return 0;  

}  

 

你可能感兴趣的:(move)