zoukankan      html  css  js  c++  java
  • 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;

    }

  • 相关阅读:
    __all__ = ["a"]被调用时只会调用list里面的
    if __name__ == "__main__"
    异常处理
    python
    python传智播客笔记--第十天:隐藏属性,私有属性,私有方法,__del__方法,类的继承,类中方法的重写
    python获取引用对象的个数
    对象属性会保留
    python中的不定长参数
    python的全局变量
    python实现文件命名
  • 原文地址:https://www.cnblogs.com/yejinru/p/2407903.html
Copyright © 2011-2022 走看看