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,在你计算出最少位置之前,牛早跑了。

然后反应过来开标记,来节约时间,然后发现居然RE了,估计是标记数组不够大,毕竟有一个方向的x * 2。

然后打算控制一下查找范围,因为当X到达某个范围之后,在变为K,肯定不会是最少路径。

比如,当X已经大于K了,再执行X + 1, X * 2,肯定不会最短的得到K,然后在判断标记的时候做了优化。

然后就过掉了。

我再去百度,看看有没有更好的方法,发现我这个方法就叫剪枝啊 = =。吓尿、

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define LEN 1000000
struct node
{
    int num,t;
}q[LEN];

bool vis[10000000];

int bfs (int x,int k)
{
    int s = 0,e = 0;

    q[s].num = x;
    q[s++].t = 0;
    vis[x] = 1;
    while (s != e)
    {
        struct node tmp = q[e++];
        e %= LEN;

        if (tmp.num == k)
            return tmp.t;

        if (!vis[tmp.num + 1] && tmp.num <= k)
        {
            q[s].num = tmp.num + 1;
            q[s++].t = tmp.t +1;
            s %= LEN;
            vis[tmp.num + 1] = 1;
        }

        if (!vis[tmp.num - 1] && tmp.num - 1 >= 0)
        {
            q[s].num = tmp.num - 1;
            q[s++].t = tmp.t +1;
            s %= LEN;
            vis[tmp.num - 1] = 1;
        }

        if (!vis[tmp.num * 2] && tmp.num <= k)
        {
            q[s].num = tmp.num * 2;
            q[s++].t = tmp.t +1;
            s %= LEN;
            vis[tmp.num * 2] = 1;
        }
    }

    return -1;
}

int main()
{
    int x,k;

    while (~scanf ("%d%d",&x,&k))
    {
        int ans = bfs (x,k);

        printf ("%d\n",ans);
    }
    return 0;
}

我的博客:http://blog.csdn.net/codehypo

POJ 3278 Catch That Cow(BFS 剪枝),布布扣,bubuko.com

时间: 2024-08-05 23:29:02

POJ 3278 Catch That Cow(BFS 剪枝)的相关文章

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: 46715   Accepted: 14673 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: 61826   Accepted: 19329 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 难度:1

http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=2e5+3; int n,k; int dp[max

POJ 3278 Catch That Cow(bfs)

题意:给定n,k两个数,三种操作,加一,减一,乘2,求n到k的最少步数: 思路:广搜求最少步数: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int q[500010]; int num[500010]; int bfs() { int b=0,e=0; q[e++]=n; if(n==m) return 0; while(b<e) { i

POJ 3278 Catch That Cow(BFS基础)

题目大意: 给你两个数字,n和k,对于n有三种变化规则. 1.n->n-1; 2.n->n+1; 3.n->2*n; 使得n==k,问如何需要最少的操作次数. 解题思路: 凡是有关最少操作次数的问题,就归结为bfs来求解,这道题是一个三入口式的bfs... 在搜索的过程中,一定要注意是否有需要剪纸的必要,不然蛮力上的话,必然会导致RE. 代码: # include<cstdio> # include<iostream> # include<algorithm

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(广搜)

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