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

Recommend

teddy | We have carefully selected several similar problems for you: 2102 1372 1240 1072 1180

题意:给你两个位置k和n,求出从k到n的最小步数。

从k有三种方式变化方式: k+1,k-1,k*2。

队列广搜,一个vis数组查看是否访问过,一个step数组记录步数。

#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string>
#include<iostream>
#include<string.h>
#include<math.h>
#define maxn 100005
using namespace std;
int vis[maxn];//标记是否访问过;
int step[maxn];//记录步数;
queue <int > q ;
int serch(int k,int n)
{
    int flag=0,a,mark=0;
    memset(vis,0,sizeof(vis));//数组清零;
    memset(step,0,sizeof(step));//数组清零;
    q.push(k);  //将k入队;
    vis[k]=1;   //标记为1;
    while(!q.empty())   //当数组非空时;
    {
        a=q.front();    //将队列首位赋值给a;
        q.pop();        //删除队列首位;
        for(int i=1; i<=3; i++)
        {
            if(i==1)
                flag=a+1;
            else if(i==2)
                flag=a-1;
            else
                flag=a*2;
            if(flag>100000||flag<0)//越界判断;
                continue;
            if(!vis[flag])//如果标记为0;
            {
                step[flag]=step[a]+1;//当前步数等于上次步数+1;
                vis[flag]=1;//标记为1;
                q.push(flag); //入队;
                if(flag==n)//如果当前值为n;
                    return step[flag];//返回步数;
            }
        }
    }
    return 0;
    //如果是提交C++的话,要不要返回值都可以;
    //但如果是G++的话,必须要一个return 0;
    //这个是编译器的问题,具体也不是很清楚;
}
int main()
{
    int n,k;        //k追n;
    while(~scanf("%d%d",&k,&n))//在这里我写成了k追n;
    {
        if(k>n) //如果k>n,只能通过不断减一得到,也就是k-n步;
            printf("%d\n",k-n);
        else
            printf("%d\n",serch(k,n));
    }
    return 0;
}

之前关于那个return 0,wa了,好坑,换了编译器就A了。

T^T,刚刚问了远航学长关于那个C++提交,G++提交不同的问题,结果学长也不知道,我也是第一次遇到这种问题,好忧桑~~

不过学长也说没见过,他说只是在这个oj上遇到的是 double要用 %.f 好坑的OJ~~QAQ

时间: 2024-11-09 00:10:48

poj 3278 Catch That Cow 广搜的相关文章

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

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(BFS 剪枝)

题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置.而且农夫有三种移动方式,X + 1,X - 1,X * 2,问最少几步抓到牛. 开始认为很简单的,三方向的BFS就能顺利解决,然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前,牛早跑了. 然后反应过来开标记

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

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 inform

POJ - 3278 Catch That Cow 简单搜索

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 m

POJ 3278 Catch That Cow

POJ: https://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80291   Accepted: 25297 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her imm

POJ 3278 Catch That Cow(bfs)

传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 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 ≤ 10