• POJ3278 Catch That Cow —— BFS


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


    Catch That Cow
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 97563   Accepted: 30638

    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.

    Source




    题解:

    一开始以为是数学找规律,但是看到数据范围很小,n<=1e5,可以用数组存;而且题目要求的是“最少步数”,而“最少步数”经常都是用BFS求的。再将BFS的思想带入看看,发现可以解决问题。

    在第一次提交时,RUNTIME ERROR了。但是再看看代码,数组没有开小,不是数组的问题。后来发现再判断某个位置是否vis时,先判断了是否vis,然后再判断这个位置是否合法,这样会导致数组溢出,所以问题就出现在这里了,需谨慎!!


    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 1e5+10;
    19 
    20 int vis[MAXN];
    21 
    22 struct node
    23 {
    24     int val, step;
    25 };
    26 
    27 queue<node>que;
    28 int bfs(int n, int k)
    29 {
    30     ms(vis,0);
    31     while(!que.empty()) que.pop();
    32 
    33     node now, tmp;
    34     now.val = n;
    35     now.step = 0;
    36     vis[n] = 1;
    37     que.push(now);
    38 
    39     while(!que.empty())
    40     {
    41         now = que.front();
    42         que.pop();
    43 
    44         if(now.val==k)
    45             return now.step;
    46 
    47         tmp.step = now.step+1;
    48         if(now.val+1>=0 && now.val+1<=1e5 && !vis[now.val+1] )  //先判断范围再判断vis !!!
    49             vis[now.val+1] = 1, tmp.val = now.val+1, que.push(tmp);
    50         if(now.val-1>=0 && now.val-1<=1e5 && !vis[now.val-1] )
    51             vis[now.val-1] = 1, tmp.val = now.val-1, que.push(tmp);
    52         if(now.val*2>=0 && now.val*2<=1e5 && !vis[now.val*2] )
    53             vis[now.val*2] = 1, tmp.val = now.val*2, que.push(tmp);
    54     }
    55 }
    56 
    57 int main()
    58 {
    59     int n, k;
    60     scanf("%d%d",&n, &k);
    61     cout<< bfs(n,k) <<endl;
    62 }
    View Code


  • 相关阅读:
    CSS样式—绝对定位(absolute)与相对定位(relative)的区别(图示)
    firefox与IE透明度(opacity)设置区别
    nopCommerce学习之架构(三)
    nopCommerce学习之程序包(四)
    Hadoop 部署
    nopCommerce学习之汉化(二)
    PetShop 首页
    nopCommerce学习之安装配置(一)
    C# 中ToString()的用法
    对象的当前状态使该操作无效
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538598.html
Copyright © 2020-2023  润新知