20190122 POJ 3278 Catch That Cow(bfs,标记)

20190122 POJ 3278 Catch That Cow(bfs,标记)

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 127921 Accepted: 39759

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.

Source

USACO 2007 Open Silver

题解

搜就完事儿了

Codes

ac代码

方法一:AC,用Node结构体存储信息,缺点,时间长,内存大

172ms,2824kB,1373

#pragma warning(disable:4996)
#include <cstdio>
#include<iostream>
#include<queue>
//#define LOCAL
using namespace std;
const int maxn = 2*1e6+10;
const int MAX = 1e5;
typedef long long ll;
bool vis[maxn];
struct Node {
    int x, step;
};
int bfs(int n, int k)
{
    queue<Node> que;
    Node t, d;
    t.x = n;
    t.step = 0;
    while (!que.empty())
        que.pop();
    memset(vis, false, sizeof(vis));
    que.push(t);
    vis[n] = true;
    while (!que.empty())
    {
        t = que.front();
        int temp = t.x;
        if (temp == k)
            return t.step;
        que.pop();
        if (temp - 1 >= 0 && !vis[temp-1])
        {
            d.x = temp - 1;
            d.step = t.step + 1;
            que.push(d);
            vis[d.x] = true;
        }
        if (temp + 1 <= MAX && !vis[temp+1])
        {
            d.x = temp + 1;
            d.step = t.step + 1;
            que.push(d);
            vis[d.x] = true;
        }
        if (2 * temp <= MAX && !vis[2*temp])
        {
            d.x = 2 * temp;
            d.step = t.step + 1;
            que.push(d);
            vis[d.x] = true;
        }
    }
    return 0;
}
void solve()
{
    int n, k;
    cin >> n >> k;
    cout << bfs(n, k) << endl;
}

int main() 
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    solve();

    return 0;
}

方法二:直接用int类型的vis数组,既可以用来标记,也可以用来存步数,省去了结构体,时间短,占用内存小。

110ms,788kB,1134

#pragma warning(disable:4996)
#include <cstdio>
#include<iostream>
#include<queue>
//#define LOCAL
using namespace std;
const int maxn = 2 * 1e6 + 10;
const int MAX = 1e5;
typedef long long ll;
int vis[maxn];

int bfs(int n, int k)
{
    queue<int> que;
    while (!que.empty())
        que.pop();
    que.push(n);
    vis[n] = 0;
    while (!que.empty())
    {
        int temp = que.front();
        que.pop();
        if (temp == k)
            return vis[k];
        if (temp - 1 >= 0 && !vis[temp - 1])
        {
            que.push(temp - 1);
            vis[temp - 1] = vis[temp] + 1;
        }
        if (temp + 1 <= MAX && !vis[temp + 1])
        {
            que.push(temp + 1);
            vis[temp + 1] = vis[temp] + 1;
        }
        if (temp * 2 <= MAX && !vis[temp * 2])
        {
            que.push(temp * 2);
            vis[temp * 2] = vis[temp] + 1;
        }
    }
    return 0;
}
void solve()
{
    int n, k;
    scanf("%d%d", &n, &k);
    printf("%d\n", bfs(n, k));
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    solve();

    return 0;
}




猜你喜欢

转载自www.cnblogs.com/wchenglin/p/10312833.html
今日推荐