zoukankan      html  css  js  c++  java
  • hdu 1372 Knight Moves bfs搜索

    给定棋盘上的两个点,求马从起点走到终点所需的最小步数,马的走法如下(和象棋差不多),本题也是简单的BFS搜索。
    hdu 1372 Knight Moves bfs搜索 - 某年某月 - zxj015的博客
     
     
    #include <stdio.h>
    #include <iostream>
    #include <queue>
    using namespace std;
    int xf,yf,xt,yt,ans;
    int dir[8][2]={{-2,1},{2,1},{-2,-1},{2,-1},{-1,2},{1,2},{-1,-2},{1,-2}};
    bool visited[9][9];
    struct node
    {
           int x,y;
           int time;
    };
    void bfs()
    {
         int i,a1,b1,front=-1,rear=-1,xx,yy;
         node in,out;
         queue<node>que;
         in.x=xf;
         in.y=yf;
         in.time=0;
         que.push(in);
         visited[xf][yf]=true;
         while(!que.empty())
         {
           out=que.front();
           que.pop();
           if(out.x==xt&&out.y==yt)
           {
             ans=out.time;
             return;
           }
           for(i=0;i<8;i++)
           {
             xx=out.x+dir[i][0];
             yy=out.y+dir[i][1];
             if(xx<0||yy<0||xx>7||yy>7||visited[xx][yy])
              continue;
             in.x=xx;
             in.y=yy;
             in.time=out.time+1;
             visited[xx][yy]=true;
             que.push(in);
           }
         }
    }
           
    main()
    {
          int i,j,flag,a,b;
          char c1,ci,c2;
          while(scanf("%c%d%c%c%d",&c1,&xf,&ci,&c2,&xt)!=EOF)
          {
             getchar();
             memset(visited,false,sizeof(visited));
             yf=c1-97;
             yt=c2-97;
             xf--;xt--;
             bfs();
             printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,++xf,c2,++xt,ans);
          }
    }
  • 相关阅读:
    9.堆排序
    8.全排列
    37.微信跳一跳辅助开发(C语言+EasyX)
    7.图形化实现快速排序法
    codeforces 632A A. Grandma Laura and Apples(暴力)
    codeforces 633D D. Fibonacci-ish(dfs+暴力+map)
    codeforces 633B B. A Trivial Problem(数论)
    codeforces 633A A. Ebony and Ivory(暴力)
    codeforces 622B B. The Time
    codeforces 622D D. Optimal Number Permutation(找规律)
  • 原文地址:https://www.cnblogs.com/zxj015/p/2740265.html
Copyright © 2011-2022 走看看