catch that cow java_POJ3278 Catch That Cow
题目链接:https://vjudge.net/problem/POJ-3278
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.
题目大意: 花花经过长时间的研究,终于研发出了能够跃迁的宇宙飞船。现在,他想要前往致远星。假设地球和致远星都在一个坐标轴上,其中地球位于坐标n,而致远星位于坐标k。而花花的飞船支持以下两种运动方式:
飞行:在一个时间单位中,能够从坐标x移动到x-1或x+1;
跃迁:在一个时间单位中,能够直接从x跃迁到2x。
现在,花花想知道,他需要多长时间才能到达致远星?
解题思路:利用BFS求出n到k的最短距离,关键在于对搜索过程进行剪枝,省去重复的工作。
Cpp Code
#include
#include
#include
using namespace std;
const int maxn = 1e5 + 5;
int n, k;
bool vis[maxn];//标记数组
int ans[maxn];
queue q;
int bfs(int x,int k){
int cur, next;
ans[x] = 0;
vis[x] = 1;//访问后标记为1,防止重复访问
q.push(x);
while (!q.empty())
{
cur = q.front();
q.pop();
for (int i = 0; i < 3;i++){//三次转换
if(i==0){
next = cur - 1;
}
else if(i==1){
next = cur + 1;
}
else{
next = cur*2;
}
//符合条件才压入队列
if(next>=0&&next<=100000&&vis[next]==0){
vis[next] = 1;
q.push(next);
ans[next] = ans[cur] + 1;
}
if(next==k){
return ans[next];
}
}
/* code */
}
}
int main(){
cin >> n >> k;
fill(vis, vis + maxn, 0);
while (!q.empty())
{
q.pop();
}
if(n>=k){
cout << n - k << endl;
}
else{
cout << bfs(n,k) << endl;
}
return 0;
}
Java Code
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static class node{
int x;//数的值
int num;//次数
node(){}
node(int a,int b){
x=a;
num=b;
}
}
static boolean[] vis=new boolean[(int)1e5+5];//标记数组
static boolean ok(int x) {//判断函数
if(x>=0&&x<=(int)1e5&&vis[x]==false) {
return true;
}
return false;
}
public static int BFS(int n,int k) {
Queue q=new LinkedList();
vis[n]=true;//访问后标记
q.offer(new node(n,0));
node op;
while(!q.isEmpty()) {
op=q.poll();
if(op.x==k) {
return op.num;
}
int x,num=op.num+1;
for(int i=0;i<3;i++) {//三次转换
if(i==0) {
x=op.x-1;
}
else if(i==1) {
x=op.x+1;
}
else {
x=op.x*2;
}
if(ok(x)) {
vis[x]=true;//访问后标记
q.offer(new node(x,num));
}
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,k;
n=sc.nextInt();
k=sc.nextInt();
System.out.print(BFS(n,k));
// TODO Auto-generated method stub
}
}
catch that cow java_POJ3278 Catch That Cow相关推荐
- java try catch 例子_java try catch
try catch机制非常好.那些觉得try catch不行的人,是他们自己的水平有问题,无法理解这种机制.并且这群人写代码不遵守规则,喜欢偷懒,这才造成try catch不好的错觉. 详细解释: 1 ...
- java catch throwable_如何处理异常? catch Exception OR catch Throwable
在Java中,当你需要统一处理异常的时候,你是会选择catch (Exception),还是直接catch (Throwable)? Java的异常体系 Throwable: Java中所有异常和错误 ...
- try 的 catch 和 promise 的 catch 有什么区别
一.相同点,都是获取代码错误信息的方法,而且都不能获取异步错误 1. try- catch- try {console.log(a)} catch (error) {console.log(error ...
- java catch 抛出异常_java - 在catch和最后claus中抛出异常
java - 在catch和最后claus中抛出异常 关于大学的Java问题,有这段代码: class MyExc1 extends Exception {} class MyExc2 extends ...
- java 进入catch,JAVA + try catch(FileNotFoundException e)进入catch(Exception e)?
我有一些在磁盘上创建文件的命令. 因为要创建文件的文件夹是动态的,所以我有一个catch(FileNotFoundException e).在同一个try块中,我已经有一个catch(Exceptio ...
- java: 无法将类 com.duo_tai.Cow中的构造器 Cow应用到给定类型;
异常类型:构造方法异常 异常原因:主方法里面用到的类没有对应的构造方法,导致此类异常 处理方式:在出问题的类里面添加合适的构造方法,下面这个异常是因为创建这个类的时候,人工重载了构造方法,默认的构造方 ...
- python try catch语句_Java try catch语句
在 Java 中通常采用 try catch 语句来捕获异常并处理.语法格式如下: try { 逻辑代码块1; } catch(ExceptionType e) { 处理代码块1; } 在以上语法中, ...
- 【try……catch】C++ try…… catch 笔记(C语言中也可以Try-Catch异常处理)
目录 C++ try--catch catch(CException *e) 打印异常信息 不经意间的内存泄露:try-catch(CException *e) [异常对象需要手动释放] C++ tr ...
- java try 没有catch_Java Try Catch Finally块没有Catch
我在复习一些新的代码.该程序只有一个try和finally块.由于catch块被排除在外,如果遇到异常或任何可丢弃的情况,try块如何工作?它是直接去最后一个街区吗? Try Finally和Try ...
最新文章
- AI面试官也太好骗了吧!
- 如何使用hyperopt对xgboost进行自动调参
- STM32(三)-------流水灯(标准库函数)
- 【POJ】1067 取石子游戏(博弈论)
- docker中使用的镜像加速器可以自己生成
- spark报错:invalid token
- 脱发、秃头防不胜防?这里有一份给码农的减压指南
- SpingMVC 注解@RequestMapping、@SuppressWarnings、@Scheduled 定时器
- 【运筹学】产销平衡问题的表上作业法
- Altium Designer——AD画PCB图步骤总结
- JS方式 获取微信openid 【html5+jquery】
- python tk/ttk制作 安卓群控助手,多台设备多任务多线程执行
- AspCms标签手册
- 【代码实践】使用CLIP做一些多模态的事情
- 计算机在运行 显示器出现黑屏,显示器黑屏但电脑一直在运行是什么原因
- daydream手柄
- Hello hello world♥
- pandas——描述性统计方法和时间类型
- Xen I/O虚拟化原理——Xen下总线、设备和驱动
- 前端有哪些组件库参考?