HDU 1372 Knight Moves

http://acm.hdu.edu.cn/showproblem.php?pid=1372

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

struct node{
    int xx,yy;
    int step;
}info;
char str1[3],str2[3];
int dir[8][2]={{2,-1},{1,-2},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
int map[10][10];
int x1,x2,y1,y2;
int flag;
void bfs(int x,int y){
    info.xx=x;
    info.yy=y;
    info.step=0;
    map[x][y]=1;
    queue<node> q;
    q.push(info);
    while(!q.empty()){
        node temp=q.front();
        q.pop();
        if( temp.xx==x2 && temp.yy==y2){
            printf("To get from %s to %s takes %d knight moves.\n",str1,str2,temp.step);
            return ;
        }

        for(int i=0;i<8;i++){
            int xx=temp.xx+dir[i][0];
            int yy=temp.yy+dir[i][1];
            int step=temp.step+1;
            if( xx>=0 && xx<8 && yy>=0 && yy<8 && map[xx][yy]==0){
                info.xx=xx;
                info.yy=yy;
                info.step=step;
                map[xx][yy]=1;
                q.push(info);
                //printf("step=%d i=%d xx=%d yy=%d\n",step,i,xx,yy);
            }
        }
    }
}
int main()
{
    while(scanf("%s %s",&str1,&str2)!=EOF){
        x1=str1[0]-97;
        y1=str1[1]-'0'-1;
        x2=(int)(str2[0]-97);
        y2=str2[1]-'0'-1;
        //printf("%d %d %d %d\n",x1,y1,x2,y2);
        memset(map,0,sizeof(map));
        bfs(x1,y1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值