BFS

Description

Download as PDF
 

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 Specification

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 Specification

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

 

Sample Output

 

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.





思路:要解这道题,首先必须明白国际象棋中骑士的走法,如图所示

然后可以使用广度优先搜索,广搜在寻求最短路径比深搜好很多,他只需入队列一次,而且只要找到了,即为最短路径,时间性能也优越。

#include"iostream"
#include"cstring"
#include"cstdio" using namespace std; struct node { int x; int y; int s; }; int nex[8][2]={{-2,1},{-2,-1},{2,-1},{2,1},{1,-2},{-1,-2},{-1,2},{1,2}}; int main() { char s1[5],s2[5]; while(scanf("%s%s",s1,s2)!=EOF) { struct node que[100]; int a[9][9],book[9][9]; memset(a,0,sizeof(a)); memset(book,0,sizeof(book)); for(int kk=0;kk<100;kk++) { que[kk].x=0; que[kk].y=0; que[kk].s=0; } int head,tail; int i,j,k,n,m,startx,starty,p,q,tx,ty,flag; startx=s1[0]-'a'+1; starty=s1[1]-'0'; p=s2[0]-'a'+1; q=s2[1]-'0'; //cout<<s1<<endl<<s2<<endl; //cout<<startx<<' '<<starty<<endl; //if(startx==p&&starty==q) cout<<"To get from "<<s1[0]<<s1[1]<<" to "<<s2[0]<<s2[1]<<" takes "<<0<<" knight moves"<<endl; { head=1; tail=1; que[tail].x=startx; que[tail].y=starty; que[tail].s=0; tail++; book[startx][starty]=1; flag=0; while(head<tail) { if

转载于:https://www.cnblogs.com/zsyacm666666/p/4663325.html

BFS(广度优先搜索)算法是一种用于遍历或搜索图结构的经典算法,其核心原理是从起点开始,逐层扩展搜索范围,直到找到目标节点遍历完整个图。该算法特别适用于解最短路径问题或扩散性质的区域问题[^1]。 ### BFS算法原理 BFS算法从初始状态(起点)出发,按照状态转换规则(图结构中的边),逐步遍历所有可能的状态(节点),直到找到目标状态(终点)。其核心思想是“先扩散后深入”,即每次处理当前层的所有节点,再进入下一层处理。这种逐层扩散的方式确保了BFS在首次到达目标节点时,所走的路径是最短的。 ### BFS算法实现方法 BFS算法通常使用队列(Queue)来实现,队列用于存储待处理的节点。具体步骤如下: 1. 将起点节点加入队列,并标记为已访问。 2. 当队列不为空时,取出队列中的第一个节点。 3. 对当前节点进行处理,例如检查是否为目标节点。 4. 遍历当前节点的所有相邻节点,如果未被访问,则标记为已访问,并加入队列。 5. 重复步骤2-4,直到找到目标节点或队列为空。 以下是一个简单的BFS算法实现示例,用于遍历图结构: ```python from collections import deque def bfs(graph, start): visited = set() # 用于记录已访问的节点 queue = deque([start]) # 初始化队列 visited.add(start) # 标记起点为已访问 while queue: node = queue.popleft() # 取出队列中的第一个节点 print(node) # 处理当前节点 # 遍历当前节点的所有相邻节点 for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) # 标记为已访问 queue.append(neighbor) # 加入队列 ``` ### BFS算法的复杂度分析 BFS算法时间复杂度和空间复杂度均图中的节点数和边数相关。假设图中有 $V$ 个节点和 $E$ 条边,则时间复杂度为 $O(V + E)$,空间复杂度为 $O(V)$。这是因为BFS需要访问所有节点和边,并且队列可能存储最多 $V$ 个节点[^1]。 ### BFS算法的应用场景 BFS算法广泛应用于以下问题: 1. **走迷宫最短路径**:寻找从起点到终点的最短路径。 2. **数字按规则转换的最少次数**:例如,将一个数字转换为另一个数字所需的最少操作次数。 3. **棋盘上某个棋子N步后能到达的位置总数**:计算棋子在N步内可以到达的所有位置。 4. **病毒扩散计算**:模拟病毒在人群中的扩散过程。 5. **图像中连通块的计算**:识别图像中的连通区域[^1]。 ### BFSDFS的比较 - **BFS**:通过队列实现,适合解决最短路径问题,但空间复杂度较高。 - **DFS**:通过递归或栈实现,适合解决需要遍历完整棵树的问题,但时间复杂度较高。 例如,在满二叉树的情况下,BFS的空间复杂度为 $O(N)$,而DFS的空间复杂度为 $O(\log N)$[^2]。 ### BFS的优势局限性 - **优势**:BFS可以保证首次到达目标节点时的路径是最短的,适用于最短路径问题。 - **局限性**:BFS的空间复杂度较高,尤其在处理大规模图时,可能需要较多的内存资源[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值