跳马~~~HDU1372

基础BFS,水过就好~手写队列优化~~

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const double pi = acos(-1.0);
const int mod =9973;

const int N =1e1+10;

struct asd{
    int x,y;
    int step;
};
asd no,ne;
asd q[N*N];
int sx,sy,ex,ey;
int dx[8]={-1,-1,-2,-2,1,1,2,2};
int dy[8]={2,-2,1,-1,-2,2,-1,1};
bool vis[N][N];



int BFS()
{
    memset(vis,0,sizeof(vis));
    int head,tail;
    head=0;
    tail=1;
    q[head].x=sx;
    q[head].y=sy;
    vis[sx][sy]=1;
    while(head<tail)
    {
        int ax=q[head].x;
        int ay=q[head].y;
        if(ax==ex&&ay==ey)
            return q[head].step; 
        for(int i=0;i<8;i++)
        {
            int aa=dx[i]+ax;
            int bb=dy[i]+ay;
            if(aa<0||aa>7||bb<0||bb>7||vis[aa][bb])
                continue;
            vis[aa][bb]=1;
            q[tail].x=aa;
            q[tail].y=bb;
            q[tail].step=q[head].step+1;
            tail++;
        }
        head++;
    }
}

int main()
{
    char a[5],b[5];
    while(~scanf("%s%s",a,b))
    {
        memset(vis,0,sizeof(vis));
        sx=a[0]-'a';
        sy=a[1]-'0'-1;
        ex=b[0]-'a';
        ey=b[1]-'0'-1;
        int qq;
        qq=BFS();
        printf("To get from %s to %s takes %d knight moves.\n",a,b,qq);
    }
    return 0;
}
posted @ 2016-07-05 19:44  see_you_later  阅读(165)  评论(0编辑  收藏  举报