初学深搜广搜的总结

来源:互联网 发布:组策略windows update 编辑:程序博客网 时间:2024/05/21 11:37
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 44969 Accepted: 14059

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (0 ≤N ≤ 100,000) on a number line and the cow is at a pointK (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 orX+ 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N andK

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

Source

USACO 2007 Open Silver

 

 

这道题提交第13次才过。这道题运用广搜,分三条路走,感觉不难。前期是Memory Limit Exceeded,超出内存。然后我就想减少访问的点,根据数学分析在if中添加条件不去访问不必访问的点,可是仍然超内存。于是在群里问了一下巨们,得知要开一个表来记录访问过的点,以免重复访问占用内存,于是就解决了。接着就出现了Runtime Error,这个就很头疼了,修改并再次提交了很多次,依然Runtime Error。最后把之前自己在if里添加的条件去掉,才ac。此次,我学到了建立一个数组,作为一个记录每个点是否访问过的表;还有在if语句里的条件如果很多很乱,十分容易出错,如果提交后没过,有可能检查if里的条件有问题。

 

 

 

 

Knight Moves
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11126 Accepted: 6277

Description

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 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 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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.

Source

Ulm Local 1996

 

 

关于骑士跳跃的问题,要对某个点分八个方向广搜,并判断是否到了棋盘之外或者访问了已经访问过的点。最后wa了两次,是因为发生了两个NC,记得其中一个是本应该-1却写成了-2,这样该方向就不是走“日”,而是逗比地走“田”字。

 

 

 

 

迷宫问题
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7462 Accepted: 4358

Description

定义一个二维数组:
int maze[5][5] = {0, 1, 0, 0, 0,0, 1, 0, 1, 0,0, 0, 0, 0, 0,0, 1, 1, 1, 0,0, 0, 0, 1, 0,};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

 

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0

Sample Output

(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)

Source

 

 

这个走迷宫的问题,运用广搜,我有信心写好它。对我而言就是如何记录路径的问题。肯定是要开个表记录路径,记录每个点的前一个点,最后由终点的点,就能找到来的路。然后大概有两个办法搞定输出:一是把起点当终点,终点当起点,这样就可以正序输出,我就是采取了这个办法;二是由起点出发,记录路径到终点后用递归倒序输出,这个问题我也还只是想想,没去实现。。。。。。。。。。。。。。

 

 

棋盘问题
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 21863 Accepted: 10864

Description

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 ,  k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

 

2 1#..#4 4...#..#..#..#...-1 -1

Sample Output

21

 

 

这个棋盘问题,我参考了八皇后的解决办法。添加已摆放棋子数的参数put,如果put==k,则完成一次深搜。还有一点是每行可以放棋子或不放棋子,若不放棋子,直接行数加一进入下次递归。

 

 

 

 

 

 

 

 

0 0
原创粉丝点击