catch the cow 抓住小黄牛
来源:互联网 发布:淘宝3c证书编号查询 编辑:程序博客网 时间:2024/05/02 02:28
/*
农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:
1、从X移动到X-1或X+1,每次移动花费一分钟
2、从X移动到2*X,每次移动花费一分钟
假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?
*/
//java
import java.util.LinkedList;import java.util.Queue;
import java.util.Scanner;
public class catch_cow {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n,k;
Queue<Node> queue;
while(in.hasNext()){
n=in.nextInt();
k=in.nextInt();
queue=new LinkedList<Node>();
int walk=0;
queue.offer(new Node(n, 0));
while(!queue.isEmpty()){
Node node=queue.poll();
int x=node.x;
int step=node.step+1;
if(x+1==k||x-1==k||x*2==k){
walk=step;
break;
}
if(x*2>=0&&x*2<=100000){
queue.offer(new Node(x*2, step));
}
if((x-1)>=0&&(x-1)<=100000){
queue.offer(new Node(x-1, step));
}
if((x+1)>=0&&(x+1)<=100000){
queue.offer(new Node(x+1, step));
}
}
System.out.println(walk);
}
}
}
class Node{
int x=0;
int step=0;
public Node(int x,int step){
this.x=x;
this.step=step;
}
}
0 0
- catch the cow 抓住小黄牛
- 重做catch the cow
- Catch The Cow
- catch the cow (P3278)
- HLG Catch The Cow
- openjudge2971 catch the cow
- POJ 3278:Catch That Cow 抓住那头牛
- bzoj1646 [Usaco2007 Open]Catch That Cow 抓住那只牛
- [BZOJ1646] [Usaco2007 Open]Catch That Cow 抓住那只牛
- BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
- POJ 3278 Catch The Cow
- poj 3278 catch the cow
- catch the cow(简单题)
- toj2825 catch the cow BFS典型题
- POJ-3278 Catch the cow(BFS)
- catch the cow(poj-3728)
- 广度优先搜索bfs与抓住那头奶牛(Catch that cow, poj3278)
- 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛
- adb shell下查看sqlite数据库
- 个人在线文档记录
- CentOS下系统时间同步和时区的修改和设置
- Ubuntu change default sh from dash to bash
- c#语法总结1
- catch the cow 抓住小黄牛
- python 抽象方法 抽象类实现
- Spring-data-jpa+Hibernate 各种缓存的配置
- h264视频编码的MP4格式视频无法在网页中的adobe flash player中播放
- 文章标题
- Java多区域部署时能自动升级的一个小演示程序
- kindeditor html代码过滤(丢失某些HTML标签)
- Tessellation Overview
- 2014年5月欧洲地区SAT写作真题及解题技巧