Catch That Cow

Description

农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) 。农夫有两种移动方式:
1、从 X移动到 X-1或X+1 ,每次移动花费一分钟
2、从 X移动到 2*X ,每次移动花费一分钟
假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?

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  

代码一:C++STL&&bfs版本:

代码:

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std; const int maxn=100001; bool vis[maxn];//标记数组
int step[maxn];//记录到了每一位置所走的步数
queue <int> q;//定义队列 int bfs(int n,int k)
{
int head,next;
q.push(n); //开始FJ在n位置,n入队
step[n]=0;
vis[n]=true; //标记已访问
while(!q.empty()) //当队列非空
{
head=q.front(); //取队首
q.pop(); //弹出对首
for(int i=0;i<3;i++) //FJ的三种走法
{
if(i==0) next=head-1;
else if(i==1) next=head+1;
else next=head*2;
if(next<0 || next>=maxn) continue; //排除出界情况
if(!vis[next]) //如果next位置未被访问
{
q.push(next); //入队
step[next]=step[head]+1; //步数+1
vis[next]=true; //标记已访问
}
if(next==k) return step[next]; //当遍历到结果,返回步数
}
}
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(step,0,sizeof(step));
memset(vis,false,sizeof(vis)); while(!q.empty()) q.pop(); //注意调用前要先清空
if(n>=k) cout<<n-k<<endl;
else cout<<bfs(n,k)<<endl;
}
}

代码二:C语言+bfs+模拟队列版本

代码;

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100010
struct node{
int x,step;
}q[N];
int vis[N];
int bfs(int n,int k){
node now,next;
int head=0,tail=1;
q[head].x=n;
q[head].step=0;
vis[n]=1;
while(head<tail){
now=q[head++];
for(int i=0;i<3;i++){
if(i==0) next.x=now.x-1;
else if(i==1) next.x=now.x+1;
else if(i==2) next.x=now.x*2;
if(next.x<0||next.x>N) continue;
if(!vis[next.x]){
vis[next.x]=1;
next.step=now.step+1;
q[tail++]=next;
}
if(next.x==k) return next.step;
} }
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
if(n>=k) printf("%d\n",n-k);
else printf("%d\n",bfs(n,k));
return 0;
}

Catch That Cow----BFS的更多相关文章

  1. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  3. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  4. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  5. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  6. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  7. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  8. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  9. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  10. POJ3278 Catch That Cow(BFS)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

随机推荐

  1. CentOS7 快速安装配置mysql8.0

    因为这个项目是两台CentOS7虚拟机,一台当作 MySQL服务器,所以需要配置3306端口公开 参考教学视频:Java2020体系课 22周课 5-2~3 两节课 yum search mysql- ...

  2. Servlet基本知识

    Servlet基本知识 1.IDEA创建第一个Servlet程序xing 这里说明如何使用 IDEA Ultimate 2020.1.3版本来新建第一个web程序.参考 MoonChasing 1.1 ...

  3. Java之JSTL标签与JavaBean

    Java之JSTL标签与JavaBean JSP.JSTL标签及 EL表达式 <!-- jstl表达式--> <dependency> <groupId>javax ...

  4. Pikachu-php反序列化、XXE、SSRF模块

    一.PHP反序列化 理解这个漏洞需要明白php中的两个函数,serialize(),unserialize() 1)序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下 ...

  5. 08-SpringCloud Consul

    Consul简介 官网 Consul下载地址 What is Consul? Consul is a service mesh solution providing a full featured c ...

  6. Python小白的数学建模课-18.最小生成树问题

    最小生成树(MST)是图论中的基本问题,具有广泛的实际应用,在数学建模中也经常出现. 路线设计.道路规划.官网布局.公交路线.网络设计,都可以转化为最小生成树问题,如要求总线路长度最短.材料最少.成本 ...

  7. 微信小程序全局数据globalData的使用问题

    如果在A页面设置全局属性,但在B页面无法使用的话,可能是这个问题: app.js globalData: { helpPage:0, }, A页面 A(e) { getApp().globalData ...

  8. mysql 优化面试题

    第一方面:30种mysql优化sql语句查询的方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by涉及的列上建立索引. 2.应尽量避免在 where 子句中使用 ...

  9. C++ leetcode接雨水

    双指针算法"接雨水" 链接:https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱 ...

  10. Quartz任务调度(2)CronTrigger定制个性化调度方案

    Cron表达式 1. 时间字段与基本格式 Cron表达式有6或7个空格分割的时间字段组成: 位置 时间域名 允许值 允许的特殊字符 1 秒 0-59 ,-*/ 2 分支 0-59 ,-*?/ 3 小时 ...