POJ 3728 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题目大意:一个农民找他的牛,告诉你 他和牛的位置(牛位置保持不动),他有三种走法 往前一步,往后一步和走到他当前位置一倍的位置(卧槽这就是个bug,怎么可能跑那么快)每次动一次需要一分钟,问你最短需要多少分钟找到他的牛简单的bfs
 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 #define M 100010
 6 #define X 100000
 7 struct place
 8 {
 9     int x,step;
10 };
11 bool vid[M];//记录是否走过
12 int main()
13 {
14     int bfs(place n,place k);
15     place n,k;
16     n.step=0;
17     while(cin>>n.x>>k.x)
18         cout<<bfs(n,k)<<endl;
19     return 0;
20 }
21 int bfs(place n,place k)
22 {
23     if(n.x==k.x)//先判断是否在相同的位置
24         return 0;
25     memset(vid,0,sizeof(vid));
26     queue<place>q;
27     place x;
28     while(1)
29     {
30         x.step=n.step+1;
31         if(n.x<k.x)//剪枝,只有当当前位置小于牛的位置才往前走
32         {
33             x.x=n.x*2;
34             if(x.x<=X&&!vid[x.x])
35             {
36                 if(x.x==k.x)//bfs最好还是先判断不然会多花时间
37                     return x.step;
38                 q.push(x);vid[x.x]=true;
39             }
40             x.x=n.x+1;
41             if(!vid[x.x])
42             {
43                 if(x.x==k.x)
44                     return x.step;
45                 q.push(x);vid[x.x]=1;
46             }
47         }
48         x.x=n.x-1;
49         if(!vid[x.x]&&x.x>=0)//不要以为只有大于才往后走,可能出现你往后走一步之后全翻倍走得情况
50         {
51             if(x.x==k.x)
52                 return x.step;
53             q.push(x);
54             vid[x.x]=1;
55         }
56         n=q.front();
57         q.pop();
58     }
59 }
60 /*
61 附上几组比较重要的数据
62 0 100000
63 22
64 100000 0
65 100000
66 7 3072
67 10
68 */
时间: 2024-10-12 05:17:53

POJ 3728 Catch That Cow的相关文章

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

POJ 3278 Catch That Cow(BFS 剪枝)

题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置.而且农夫有三种移动方式,X + 1,X - 1,X * 2,问最少几步抓到牛. 开始认为很简单的,三方向的BFS就能顺利解决,然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前,牛早跑了. 然后反应过来开标记

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

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

POJ: https://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80291   Accepted: 25297 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her imm

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

POJ 3278 Catch That Cow(bfs)

传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 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 ≤ 10

POJ - 3278 - Catch That Cow (BFS)

题目传送:Catch That Cow 思路:BFS找最小步数,用一个结构体存下当前结点的数值以及当前步数 AC代码: #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath> #include <queue>

POJ 题目Catch That Cow(BFS)

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