HDU 1372 Knight Moves

来源:互联网 发布:python rexec 的使用 编辑:程序博客网 时间:2024/05/20 18:41

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
原创粉丝点击