uva439 暂时未AC代码

来源:互联网 发布:知行乐学教育集团 编辑:程序博客网 时间:2024/05/17 03:56
#include <iostream>#include <cstdio>#include <queue>#include <string.h>using namespace std;struct Node{    int x;    int y;    int step;};Node vs;Node vd;char ch;int m;char ch1;int m1;int ans;queue <Node> q;bool visit[10][10];int dir[8][2]= {-1,-2,-1,2,-2,-1,-2,1,1,2,1,-2,2,1,2,-1};void initial(){    ans=0;    memset(visit,0,sizeof(visit));    vs.x=m;    vs.y=ch-'a';    vs.step=0;    vd.x=m1;    vd.y=ch1-'a';    vd.step=0;    while(!q.empty())        q.pop();}void bfs(){    Node vn,vw;    q.push(vs);    visit[vs.x][vs.y]=true;    while(!q.empty())    {        vn=q.front();        q.pop();        for(int j=0; j<8; j++)        {            vw.step=vn.step+1;            vw.x=vn.x+dir[j][0];            vw.y=vn.y+dir[j][1];            if(vw.x==vd.x && vw.y==vd.y)            {                ans=vw.step;                return ;            }            if(!visit[vw.x][vw.y] && (vw.x>0 && vw.x<=8 && vw.y>0 && vw.y<=8))            {                q.push(vw);                visit[vw.x][vw.y]=true;            }        }    }    return ;}int main(){    while(~scanf(" %c%d %c%d",&ch,&m,&ch1,&m1))    {        initial();        bfs();        if(ch==ch1 && m==m1)        {            printf("To get from %c%d to %c%d takes 0 knight moves.\n",ch,m,ch1,m1);        }        else        {            printf("To get from %c%d to %c%d takes %d knight moves.\n",ch,m,ch1,m1,ans);        }    }    return 0;}

测试了很多组数据 都对……但是就是没有AC……留作以后再改

原创粉丝点击