UVA 439 BFS入门

来源:互联网 发布:域名在微信内禁止访问 编辑:程序博客网 时间:2024/06/01 10:04

题意

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

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 30using 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 30using 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')));    }}

0 0