我是靠谱客的博主 唠叨电灯胆,最近开发中收集的这篇文章主要介绍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("%dn",k-n);
else
printf("%dn",serch(k,n));
}
return 0;
}

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

T^T,刚刚问了远航学长关于那个C++提交,G++提交不同的问题,结果学长也不知道,我也是第一次遇到这种问题,好忧桑~~
不过学长也说没见过,他说只是在这个oj上遇到的是 double要用 %.f 好坑的OJ~~QAQ

最后

以上就是唠叨电灯胆为你收集整理的poj 3278 Catch That Cow 广搜的全部内容,希望文章能够帮你解决poj 3278 Catch That Cow 广搜所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部