首页 > 代码库 > uva 439

uva 439

bfs 最近感觉自己渣的一匹

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=90;
int x1,x2,y1,y2;
char c1,c2;
int bb[10][5]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{2,1},{1,-2},{2,-1}};
int vis[10][10];
struct queuei
{
   int a,b,d;
    queuei(int x,int y,int z):a(x),b(y),d(z) {}
    queuei() {}
}qq[maxn];
int dfs(int x,int y,int xx,int yy)
{
    if(x==xx&&y==yy) return 0;
    memset(vis,0,sizeof(vis));
    qq[0]=queuei(x,y,0);
    int move=0,save=1;
    while(move<save)
    {
        queuei _new=qq[move++];
        for(int i=0;i<8;i++)
        {
            x=_new.a+bb[i][0];
            y=_new.b+bb[i][1];
            if(!vis[x][y]&&x>0&&x<9&&y>0&&y<9)
            {
                if(x==xx&&y==yy) return _new.d+1;
                vis[x][y]=1;
                qq[save++]=queuei(x,y,_new.d+1);
            }
        }
    }
    return -1;
}
int main()
{
    while(cin>>c1>>y1>>c2>>y2)
    {

        x1=(int)(c1-a+1);
        x2=(int)(c2-a+1);
        printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,y1,c2,y2,dfs(x1,y1,x2,y2));
    }
    return 0;
}

 

uva 439