hoj1440Knight Moves

/*

题目:

       象棋中马如何走到指定地点

分析:

       BFS题,分8个方向搜就行

  具体的图型可以看看poj1915题的8个方向,理解BFS后很容易写出

*/

#include <iostream>

#include <cstring>

#include <queue>

#include <cstdio>

using namespace std;

#define X 310

int sx,sy,ex,ey,n;

bool visit[X][X];

struct node

{

       int x,y,step;

};

bool check(int x,int y)

{

       if(x<1||y<1||x>n||y>n)

              return false;

       return true;

}

void bfs()

{

       ++sx;

       ++sy;

       ++ex;

       ++ey;

 

       queue<node>  q;

       node a;

       node b;

       a.step = 0;

       a.x = sx;

       a.y = sy;

       visit[sx][sy] = true;

       q.push(a);

      

       int x,y,s;

 

       while(!q.empty())

       {

              b = q.front();

              q.pop();

              sx = b.x;

              sy = b.y;

              s = b.step;

              if(sx==ex&&sy==ey)

              {

                     cout<<s<<endl;

                     return;

              }

 

              x = sx-2;

              y = sy-1;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

 

              x = sx-2;

              y = sy+1;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

 

              x = sx-1;

              y = sy-2;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

             

              x = sx-1;

              y = sy+2;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

             

              x = sx+1;

              y = sy-2;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

             

              x = sx+1;

              y = sy+2;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

             

              x = sx+2;

              y = sy-1;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

             

              x = sx+2;

              y = sy+1;

              if(check(x,y)&&!visit[x][y])

              {

                     visit[x][y] = true;

                     a.step = s+1;

                     a.x = x;

                     a.y = y;

                     q.push(a);

              }

       }

}

int main()

{

       freopen("sum.in","r",stdin);

       freopen("sum.out","w",stdout);

       int t;

       cin>>t;

       while(t--)

       {

              memset(visit,false,sizeof(visit));

              scanf("%d%d%d%d%d",&n,&sx,&sy,&ex,&ey);

              bfs();

       }

       return 0;

}

你可能感兴趣的:(move)