bfs 经典例题:骑士游历

我们先来了解一下宽度优先搜索算法:

  宽度优先搜索又译为广度优先搜索算法(英语:Breadth-First-Search,缩写为BFS),或横向优先搜索,是一种图形搜索算法。简单的说,BFS是从根节点开始,沿着树的宽度遍历树的节点。如果所有节点均被访问,则算法中止。

它的思想是:从图中某顶点v出发,在访问了v之后依次访问v的各个未曾访问过的邻接点,然后分别从这些邻接点出发依次访问它们的邻接点,并使得“先被访问的顶点的邻接点先于后被访问的顶点的邻接点被访问,直至图中所有已被访问的顶点的邻接点都被访问到。

如果此时图中尚有顶点未被访问,则需要另选一个未曾被访问过的顶点作为新的起始点,重复上述过程,直至图中所有顶点都被访问到为止。

话不多说了上题目:

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy. 
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. 

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b. 

Input

The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard. 

Output

For each test case, print one line saying "To get from xx to yy takes n knight moves.". 

Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
//从题中我们很容易确定是用bfs来操作的。

代码如下:   我是选择用数据结构中的队列来做的。

# include <cstdio>
# include <iostream>
# include <cstring>
# include <queue>
using namespace std;
int begainx,begainy,endx,endy;
const int MAX = 10+5;
const int dx[MAX] = {-2,-2,2,2,-1,-1,1,1};
const int dy[MAX] = {1,-1,-1,1,2,-2,-2,2};
int vis[MAX][MAX],m; 
struct point{    //结构体的用法:在结构体中添加函数,之后可以直接用函数来表示一个结构体。
    int x,y,step;
    point (int xx,int yy,int dd){
        x=xx;
        y=yy;
        step=dd;
    }
};
void bfs(int x,int y,int step)
{
  queue<point>q;
  q.push(point(x,y,step));
  vis[x][y]=1;
  while (!q.empty()){
          point p=q.front();
          q.pop();
          if (p.x==endx&&p.y==endy){
              m=p.step;
              break;
        }
          for (int i=0;i<8;++i){   //从八个方向来进行bfs
              int a=p.x+dx[i];
            int b=p.y+dy[i];
            if (!vis[a][b]&&a>=0&&a<8&&b>=0&&b<8){  //这个条件要注意一下,很容易出错就出不来答案。
                   q.push(point(a,b,p.step+1)); //保证
                  vis[a][b]=1;
            }
        }
           
    }
}
int main ()
{
  char x1,y1,x2,y2,ch;
  while ((scanf("%c%c %c%c",&x1,&y1,&x2,&y2))!=EOF){   //读取数据这边也要注意,调试一下。
               begainx=x1-'a';   //把字符类型转换成int型方便后续的操作。
               begainy=y1-'1';
          endx=x2-'a';
          endy=y2-'1';
          memset(vis,0,sizeof(vis));
          bfs(begainx,begainy,0);
          printf ("To get from %c%c to %c%c takes %d knight moves.\n",x1,y1,x2,y2,m);
          ch=getchar(); 
    }    

bfs,水一题。

猜你喜欢

转载自blog.csdn.net/wcgwww/article/details/81431055
BFS