UVA 439 BFS入门

题意

国际象棋跳马 指定马的起点和终点 ,计算马最少需要多少步到达终点

Sample Input

e2 e4

a1 b2

b2 c3

a1 h8

a1 h7

h8 a1

b1 c3

f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.

To get from a1 to b2 takes 4 knight moves.

To get from b2 to c3 takes 2 knight moves.

To get from a1 to h8 takes 6 knight moves.

To get from a1 to h7 takes 5 knight moves.

To get from h8 to a1 takes 6 knight moves.

To get from b1 to c3 takes 1 knight moves.

To get from f6 to f6 takes 0 knight moves.

bfs一遍即可

但是还是WA了,思维太不严密了。。

WA 1

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 30
using namespace std;
struct node{
    int x;
    int y;
    node(int x,int y){
        this->x=x;
        this->y=y;
    }
};
int go[8][2]={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
int mp[9][9];
int bfs(node f,node e){
    queue <node> q;
    q.push(f);
    mp[f.x][f.y]=0;
    while(!q.empty()){
        node p=q.front();
        q.pop();
        int x=p.x,y=p.y;
        for(int i = 0 ; i<8 ; i++){
            int xx=x,yy=y;
            xx+=go[i][0];
            yy+=go[i][1];
            if(xx<10&&xx>0&&yy<10&&yy>0){
                if(mp[xx][yy]!=-1)
                    mp[xx][yy]=min(mp[xx][yy],mp[x][y]+1);
                else{
                    mp[xx][yy]=mp[x][y]+1;
                    q.push(node(xx,yy));
                }
                if(xx==e.x&&yy==e.y)
                    return mp[xx][yy];
            }
        }
    }
}
int main(){
    char a[4],b[4];
    while(~scanf("%s%s",a,b)){
        memset(mp,-1,sizeof(mp));
        printf("To get from %s to %s takes %d knight moves.\n",a,b,bfs(node(a[0]-'a'+1,a[1]-'0'),node(b[0]-'a'+1,b[1]-'0')));
    }
}
xx yy范围错误

AC代码

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 30
using namespace std;
struct node{
    int x;
    int y;
    node(int x,int y){
        this->x=x;
        this->y=y;
    }
};
int go[8][2]={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
int mp[9][9];
int bfs(node f,node e){
    queue <node> q;
    q.push(f);
    mp[f.x][f.y]=0;
    while(!q.empty()){
        node p=q.front();
        q.pop();
        int x=p.x,y=p.y;
        for(int i = 0 ; i<8 ; i++){
            int xx=x,yy=y;
            xx+=go[i][0];
            yy+=go[i][1];
            if(xx<9&&xx>0&&yy<9&&yy>0){
                if(mp[xx][yy]==-1){
                    mp[xx][yy]=mp[x][y]+1;
                    q.push(node(xx,yy));
                }
                if(xx==e.x&&yy==e.y)
                    return mp[xx][yy];
            }
        }
    }
    return -1;
}
int main(){
    //freopen("f:\\in.txt","r",stdin);
    //freopen("f:\\out.txt","w",stdout);
    char a[4],b[4];
    while(~scanf("%s%s",a,b)){
        memset(mp,-1,sizeof(mp));
        printf("To get from %s to %s takes %d knight moves.\n",a,b,bfs(node(a[0]-'a'+1,a[1]-'0'),node(b[0]-'a'+1,b[1]-'0')));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值