【搜索】POJ3278:Catch That Cow

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,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 - 1 or + 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.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <queue>
 6 using namespace std;
 7 const int maxn = 100010;
 8 bool vis[maxn];
 9 int step[maxn];
10
11 bool bound(int num)
12 {
13     if(num<0||num>100000)
14         return true;
15     return false;
16 }
17 int bfs(int sta,int endd)
18 {
19     queue<int> q;
20     int now,nextt;
21     q.push(sta);
22     vis[sta]=true;
23     while(!q.empty())
24     {
25         now=q.front();
26         q.pop();
27         for(int i=0;i<3;i++)
28         {
29             if(i==0)
30                 nextt=now+1;
31             else if(i==1)
32                 nextt=now-1;
33             else
34                 nextt=now*2;
35             if(bound(nextt))
36                 continue;
37             if(!vis[nextt])
38             {
39                 step[nextt]=step[now]+1;
40                 if(nextt==endd)
41                     return step[nextt];
42                 vis[nextt]=true;
43                 q.push(nextt);
44             }
45         }
46     }
47 }
48 int main()
49 {
50     int sta,endd;
51     while(scanf("%d%d",&sta,&endd)!=EOF)
52     {
53         memset(vis,false,sizeof(vis));
54         if(sta>=endd)
55             printf("%d\n",sta-endd);
56         else
57         {
58             int ans=bfs(sta,endd);
59             printf("%d\n",ans);
60         }
61     }
62     return 0;
63 }

原文地址:https://www.cnblogs.com/SoulSecret/p/8445769.html

时间: 2024-10-12 09:21:35

【搜索】POJ3278:Catch That Cow的相关文章

POJ3278——Catch That Cow

Catch That Cow DescriptionFarmer 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 l

poj3278 Catch That Cow

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,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo

【转】POJ-3278 Catch That Cow:BFS

能想到用广搜来解这道题也够diao了:广搜到 目标节点 就可以得到答案 steps #include<iostream> #include<queue> #include<cstring> using namespace std; struct node { int Value, Steps; node( int N, int S ): Value(N), Steps(S){} }; queue<node>Que; int Visited[100000];

[kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow

1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<vector> 5 #include<queue> 6 #include<cstring> 7 using namespace std; 8 int n, k, c, ans; 9 int num[100005]; 10 int main() 11 { 12 // freopen("in

POJ3278 HDU2717 Catch That Cow

问题链接:POJ3278 HDU2717 Catch That Cow. 题意简述:一条线上,人的FJ的起点为K位置,牛在N位置(牛不动),输入正整数K和N.若FJ在x位置,FJ有三种走法,分别是走到x-1.x+1或2x位置.求从K走到N的最少步数. 问题分析:典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点. 需要说明的是,除了BFS方法,这个题应该可以用分支限界法来解,需要更高的技巧. AC的C++语言程

POJ3278,Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46459   Accepted: 14574 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

poj3278(Catch That Cow)

题目大意: 一个农主寻找牛.给出农主的位置n和牛的位置k.农主可以通过n-1或者n+1或者n*2的步伐找牛,问至少多少步才能找到自己的牛. 解题思路: 简单的BFS.把农主的每一种可能的步伐通过BFS存到栈中,然后看最少多少步到达K坐标. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstri

poj 3278 Catch That Cow (bfs搜索)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 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抓住那只奶牛(BFS,广度优先搜索)

# **Catch That Cow(抓住那只奶牛)**[题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个过程是线性的)具体的题目请见:[原题链接:](http://poj.org/problem?id=3278).思路: 从题目中位置移动就可以看出是一个搜索问题,又加上要找最快的方法,也就是在暗示这个是一个广搜题,每次搜索有三个方向,是一个常规题,不过也有细节,请跟随笔者我思路. 代码: #includ

Catch That Cow(广度优先搜索_bfs)

 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48036   Accepted: 15057 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