HDU 1372 广度优先搜索(BFS) Knight Moves

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<queue>

using namespace std;
const int maxn=100000;
int dx[8]={-2,-2,-1,1,2,2,1,-1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
bool vis[10][10];
int ex,ey;

struct node{
    int x,y;
    int k;
}rt,ne;

int BFS(){
    queue<node>M;
    M.push(ne);
    memset(vis,false,sizeof(vis));
    vis[ne.x][ne.y]=true;
    while(!M.empty()){
        rt=M.front(); M.pop();
        if(rt.x==ex&&rt.y==ey)break;
        for(int i=0;i<8;++i){
            ne=rt;
            ne.x+=dx[i], ne.y+=dy[i], ++ne.k;
            if(ne.x<1||ne.y<1||ne.x>8||ne.y>8||vis[ne.x][ne.y])continue;
            M.push(ne);
            vis[ne.x][ne.y]=true;
        }
    }
    return rt.k;
}

int main(){
    char a,b;
    int x,y;
    while(~scanf("%c%d %c%d",&a,&x,&b,&y)){
        getchar();
        ex=b-'a'+1, ey=y;
        ne.x=a-'a'+1, ne.y=x, ne.k=0;
        printf("To get from %c%d to %c%d takes %d knight moves.\n",a,x,b,y,BFS());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值