Catch That Cow----BFS
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的更多相关文章
- 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,最先 ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- 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 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- POJ3278 Catch That Cow(BFS)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
随机推荐
- 关于Feign、Jackson、RabbitMQ、Jrebel插件的开发中遇到的问题
在工作实际开发中需要开发一个消息模块对外提供统一的接口feign调用提供消息加载到MQ队列的服务,采用泛型的形式. 刚开始搭建好之后,正好需要做一个全局的日志添加到zuul网关中,通过网关feign ...
- cas5.3.1 从搭建到连接mysql(简而优美)
前言: cas是单点登录服务框架,为单点登录业务提供了便捷服务,它分为client,server端,client端要聚合到我们自己的项目. server端要单独构建运行,本篇文章主要讲解一下cas5. ...
- SpringCloud升级之路2020.0.x版-14.UnderTow AccessLog 配置介绍
本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford server: u ...
- Golang语言系列-01-Go语言简介和变量
Go语言简介 Go(又称Golang)是Google开发的一种静态强类型.编译型.并发型,并具有垃圾回收功能的编程语言. 罗伯特·格瑞史莫(Robert Griesemer),罗勃·派克(Rob Pi ...
- 服务启动shell脚本
#!/bin/sh JarDir=`pwd` do_start() { echo "pandora-login start ..." nohup java -jar -Xmn256 ...
- metasploit的数据库配置
metasploit所处位置:/usr/share/metasploit-framework msf数据库连接命令:db_connect msf:msfadmin@127.0.0.1/msf 1.启动 ...
- docker部署elasticsearch-+-Kibana(6-8)-+-SpringBoot-2-1-6
elasticsearch快速开始 docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e ...
- Specification使用notin
废话不多说直接贴代码 Specification<Employee> employeeSpecification = new Specification<Employee>() ...
- Quartz任务调度(5)TriggerListener分版本超详细解析
TriggerListener 在我们的触发器监听器中,也包含了一系列监听方法 方法 说明 getName() 定义并返回监听器的名字 triggerFired() 当与监听器相关联的 Trigger ...
- JDBC中的元数据
在我编写JDBC代码的时候:出现很多的重复的代码,有没有什么办法让我们能够编写出更加通用的JDBC代码呢?使用元数据,元数据能够让我们编写出更加通用的JDBC代码.什么是元数据呢?(三种元数据)1)连 ...