• 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;

    }

  • 相关阅读:
    所有HTTP返回状态值,并说明用途
    几个简单的排序算法
    Linux命令大全
    存储过程中执行动态Sql语句
    IE8的背景不显示和图片错位 解决方案
    海量数据处理方法
    关于MSSQL的返回值问题
    SQL Server 2008不能修改表的解决方法
    转:读AD里特殊的属性in C#
    了解SMS的主要特性。
  • 原文地址:https://www.cnblogs.com/yejinru/p/2407903.html
Copyright © 2020-2023  润新知