POJ3278Catch That Cow(BFS+水题)

Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 64176   Accepted: 20156

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.

题意:n通过几次变化能到k,n可以加一,减一,乘二;

bfs搜索一遍即可

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <string.h>
 6 #include <algorithm>
 7 using namespace std;
 8 int n,k,num[100000 + 5];
 9
10 void bfs(int y)
11 {
12     queue<int> q;
13     q.push(y);
14     while(q.size())
15     {
16         int fx;
17         int x = q.front();
18         q.pop();
19         if(x == k)
20             break;
21         for(int i = 0; i < 3; i++)
22         {
23             if(i == 0)
24                fx = x + 1;
25             else if(i == 1)
26                 fx = x - 1;
27             else if(i == 2)
28                 fx = x * 2;
29             if(fx >= 0 && fx <= 100000 && num[fx] > num[x])
30             {
31                 num[fx] = num[x] + 1;
32                 q.push(fx);
33             }
34         }
35     }
36 }
37 int main()
38 {
39     while(scanf("%d%d", &n,&k) != EOF)
40     {
41         for(int i = 0; i < 100005; i++)
42             num[i] = 100000000;
43         num[n] = 0;
44         bfs(n);
45         printf("%d\n",num[k]);
46     }
47
48     return 0;
49 }

时间: 2024-08-09 21:28:02

POJ3278Catch That Cow(BFS+水题)的相关文章

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu1240 bfs 水题

原题链接 思路:水题,直接搜 1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x > 0

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

POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a c

PoJ3278--Catch That Cow(Bfs)

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

POJ3287(BFS水题)

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

POJ3126 Prime Path bfs, 水题 难度:0

题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数. 思路 质数明显需要先处理出来,然后采用bfs获取结果即可. 感想 下次需要先计算空间复杂度, 1e8的空间复

POJ 3673 Cow Multiplication (水题)

题意:给你两个数,求所有的数位的积的和. 析:太水了,没的说,可以先输入边算,也可以最后再算,一样.. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #inclu

HDU1548A strange lift BFS水题

没啥好说的,注意一下,走过的楼层不能再走,否则会陷入循环 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #inclu