首页 > 其他 > 详细

HDU 1372 Knight Moves

时间:2014-02-09 16:28:26      阅读:339      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣
#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;  
}  
bubuko.com,布布扣

HDU 1372 Knight Moves

原文:http://www.cnblogs.com/forever97/p/3541289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!