Catch That Cow 经典广搜

链接:http://poj.org/problem?id=3278

题目:

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (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 or X + 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 and
K

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.

翻译:(俺英语不行,只好看翻译了)

题意:看完中文,题意就一目了然了,意思就是农夫追牛,牛不动,农夫有三种移动方式,

第一种:前移一步;

第二种:后移一步;

第二种:农夫现在的位置X2;

每次移动都消耗1分钟,求农夫追到牛的最短时间。

思路:又是一道搜索题,我们的目标是求出农夫抓住牛的最短时间,根据我们所学的两种搜索dfs与bfs,

我觉得bfs是浪费空间节省时间,dfs是浪费时间节省空间,

所以对于这种最短路径的问题,我们使用bfs显得更加快捷。

代码:

#include <cstdio>
#include<queue>
#include<cstring>
#define position 100005
using namespace std;
int n,k;
struct num//定义结构体表示每一个位置
{
int x;//位置坐标
int step;//走到这一步的步数(我们将时间转换为步数理解,每一步需要一分钟)
};
bool visit[position];//标记,判断是否走过
void bfs()
{
queue<num> steps;//定义队列存储位置
num start,now,next;//start=起点,now=前的位置,next=下一步的位置
memset(visit,false,sizeof(visit));//初始化标记(false表示未走过)
start.x=n;//初始化起点
start.step=0;
steps.push(start);//起点入队
visit[start.x]=true;//标记起点(表示走过)
while(!steps.empty())
{
now=steps.front();//取队列首元素表示当前位置
steps.pop();//首元素出列
if(now.x==k)//如果当前位置就是牛的位置(得到结果)输出步数,退出调用函数
{
printf("%d\n",now.step);
return;
}
for(int i=0;i<3;i++)
{
if(i==0)
next.x=now.x+1;
else if(i==1)
next.x=now.x-1;
else if(i==2)
next.x=now.x*2;//对三种方法进行判定

if(next.x>=0&&next.x<position&&!visit[next.x])
{
visit[next.x]=true;//表示走过了
next.step=now.step+1;//步数加一
steps.push(next);//入队
}
}
}
}
int main()
{
scanf("%d %d",&n,&k);
bfs();
return 0;
}

注意范围

原文地址:https://www.cnblogs.com/sidenynb/p/12230438.html

时间: 2024-10-14 06:47:22

Catch That Cow 经典广搜的相关文章

poj 3278 Catch That Cow(广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

HDU2717 Catch That Cow 【广搜】

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7147    Accepted Submission(s): 2254 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

Catch That Cow (BFS广搜)

问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

poj 3278:Catch That Cow(简单一维广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

poj 3278 Catch That Cow 广搜

hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11466 Accepted Submission(s): 3551 Problem Description Farmer John has been informed of the location of a fugitive cow

HDU 2717 Catch That Cow (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

POJ 3278 Catch That Cow(BFS,板子题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00