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 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.

今晚是大年三十除夕夜,在这里祝愿大家新年快乐!在新的一年里,开开心心,工作顺利,学习更上一层楼~n(*≧▽≦*)n

题目大意:在一个数轴上有n和k,农夫在n这个位置,奶牛在k那个位置,农夫要抓住奶牛,有两种方法:1、walking即农夫可以走x+1的位置和x-1的位置。

2、teleporting即每分钟可以走到2*x的位置。利用这两种方法,找到最快几步可以到达!

解题思路:其实就这三种情况,本来没打算用搜索的,不过发现好多东西都可以灵活运用!我们吧第一种方法看成两个方向,用dir[2]={1,-1,}表示,然后进行广搜!第二种方法自然就要特殊判断一下了!有一个地方要特殊判断一下,就是因为数据比较大,vis[ss.x]可能会超范围导致RE,所以都加一行判断使其保证在题目范围中。

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <queue>
 6
 7 using namespace std;
 8
 9 int dir[3]= {1,-1};
10
11 struct node
12 {
13     int x,step;
14 } s,ss;
15
16 int bfs(int n,int k)
17 {
18     queue<node>q,qq;
19     s.x=n;
20     s.step=0;
21     int vis[100010]= {0};
22     q.push(s);
23     while (!q.empty())
24     {
25         s=q.front();
26         q.pop();
27         if (s.x==k)
28             return s.step;
29         for (int i=0; i<2; i++)
30         {
31             ss.x=s.x+dir[i];
32             ss.step=s.step+1;
33             if (ss.x>=0&&ss.x<=100000)
34                 if (!vis[ss.x])
35                 {
36                     vis[ss.x]=1;
37                     q.push(ss);
38                 }
39         }
40         ss.x=s.x*2;
41         ss.step=s.step+1;
42         if (ss.x>=0&&ss.x<=100000)
43         {
44             if (!vis[ss.x])
45             {
46                 vis[ss.x]=1;
47                 q.push(ss);
48             }
49         }
50     }
51     return 0;
52 }
53
54 int main ()
55 {
56     int n,k;
57     while (~scanf("%d%d",&n,&k))
58     {
59         printf ("%d\n",bfs(n,k));
60     }
61     return 0;
62 }
时间: 2024-08-02 14:42:05

hdu 2717 Catch That Cow(广搜bfs)的相关文章

[ACM] hdu 2717 Catch That Cow (BFS)

Catch That Cow Problem 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

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

bfs hdu 2717 Catch That Cow

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

HDU 2717 Catch That Cow(bfs)

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

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

杭电 HDU 2717 Catch That Cow

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

HDU 3085 Nightmare Ⅱ (双向广搜)

题意:有M,G两人和鬼魂(Z)在n*m的方格内,M每秒走3步,G每秒走一步,鬼魂每秒走2步,问是否能 不遇到鬼魂下两人相遇,鬼魂可以穿墙(X),人不可以.初始鬼魂有2个. #include<stdio.h> #include<string.h> #include<string> #include<queue> #include<map> #include<iostream> #include<algorithm> #def

HDU 1401 Solitaire (双向广搜)

题意:在二维8*8的方格,给定4个初始点和4个最终点,问在8步内是否能从初始点走到最终点, 规则:每个点能上下左右移动,若4个方向已经有点则可以跳到下一个点. 双向广搜:同时对初始点和最终点广搜4步,对每一步记录状态,初始点为'1',最终点为'2', 若在限定时间内初始点的状态能到达'2',或最终点的状态能到达'1',则为YES!要记得排序.. #include<stdio.h> #include<string.h> #include<queue> #include&l