广度搜索学习总结

2013年12月19日2,9950

BFS,其英文全称是Breadth First Search。 BFS并不使用经验法则算法。从算法的观点,所有因为展开节点而得到的子节点都会被加进一个先进先出的队列中。

knight moves(本题是最裸的版本)

Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.

 

 

 

 

 

 

 

 

 

 

 

输入

The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first linespecifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. Thesecond and third line contain pair of integers {0, …, l-1}*{0, …, l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

输出

For each scenario of the input you have to calculate the minimal amount of knight moves which arenecessary to move from the starting point to the ending point. If starting point and ending point areequal,distance is zero. The distance must be written on a single line.

样例输入

3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1

样例输出

5 28 0

代码

紧急救援(在地图中加入了障碍)

题目描述

话说2007年8月5日,Mike博士神秘失踪了,最后发现是被外星人绑架了,幸好外星人目前还是在地球上活动,并且知道外星人不了解地球,幸好,Milk博士身上有无线信号发送装置,我们终于确定了他的位置,必须赶快到那里去救他。

根据无线信号发送装置,我们确定出一张地图,为了尽快寻找到Mike博士,于是这个光荣和艰巨的任务便交给了你,编写程序,通过使用一张地图帮助研究所确定从研究所出发找到Mike博士最短距离。

数据范围: n<=1000

输入格式第一行为n第二行为n*n的地图(其中0表示通路,1表示死路)最后两行每行有两个数字,分别表示研究所的坐标和博士信号所在的位置。
输出格式一个数字k,表示从研究所出发找到Milk博士的最短距离。样例输入10
0100110100
0001110010
1000000001
1000100011
0000101100
1000001100
1001010011
0000010100
0101010000
1001000001
1 7
10 2样例输出14


 

武士风度的牛(本题是上两题的结合)

题目描述

这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了The Knight的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。现在你的任务是,确定The Knight要想吃到草,至少需要跳多少次。The Knight的位置用’K’来标记,障碍的位置用’*’来标记,草的位置用’H’来标记。

这里有一个地图的例子:
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . H
5 | * . . . . . . . . .
4 | . . . * . . . * . .
3 | . K . . . . . . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ———————-
1
0 1 2 3 4 5 6 7 8 9 0

The Knight 可以按照下图中的A,B,C,D…这条路径用5次跳到草的地方(有可能其它路线的长度也是5):
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . F<
5 | * . B . . . . . . .
4 | . . . * C . . * E .
3 | .>A . . . . D . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ———————-
1
0 1 2 3 4 5 6 7 8 9 0

输入

第一行:  两个数,表示农场的列数(< =150)和行数(< =150) 第二行..结尾:  如题目描述的图。

输出

一个数,表示跳跃的最小次数。

样例输入

10 11

……….

….*…..

……….

…*.*….

…….*..

..*..*…H

*………

…*…*..

.K……..

…*…..*

..*….*..

样例输出

5

提示

Hint:这类问题可以用一个简单的先进先出表(队列)来解决。

代码

这类的广搜中还有可能加入传送门等,不过大致来说是一样的

再来一道经典的问题

倒酒问题

题目描述

 

分别输入三个杯子容量a,b,c 且第一个为初始杯中的酒量,另外两个为空的。现要求你要精确量出

的e容量的酒。问最少通过几步能精确量出你所要的容量。.例如有三个烧杯容量分别为:80、50、30毫升,现在第一个杯中装满了80

毫升水,其余两个是空的。现要精确的40毫升水,不许用其它工具,请找出最少步骤的方法。 若超过100步,则认为这种计量方法太麻烦

,直接输出“no answer”

 

样例输入

80 50 30 40

样例输出

step1:30 50 0

step2:30 20 30

step3:60 20 0

step4:60 0 20

step5:10 50 20

step6:10 40 30

分析