poj3278--Catch That Cow

2024-08-25 01:38
文章标签 poj3278 catch cow

本文主要是介绍poj3278--Catch That Cow,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Catch That Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 43680 Accepted: 13615

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 - 1 or + 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
bfs可以直接搜到,三种情况+1  -1  *2  同时判断不能超出范围
#include <stdio.h>
#include <string.h>
int p[1000000] ;
int a[1000000] , top , low ;
int main()
{int n , k ;memset(p,-1,sizeof(p));scanf("%d %d", &n, &k);top = 0  ; low = 0 ;a[top++] = n ;p[n] = 0 ;while(low < top){int s = a[low++] ;if(s == k)break;if( s - 1 >= 0 && p[s-1] == -1){p[s-1] = p[s]+1 ;a[top++] = s - 1 ;}if(s + 1 < 1000000 && p[s+1] == -1){p[s+1] = p[s]+1 ;a[top++] = s+1 ;}if(s * 2 < 1000000 && p[s*2] == -1){p[s * 2] = p[s] +1 ;a[top++] = s * 2;}}printf("%d\n", p[k]);return 0;
}


这篇关于poj3278--Catch That Cow的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104190

相关文章

C++第四十七弹---深入理解异常机制:try, catch, throw全面解析

✨个人主页: 熬夜学编程的小林 💗系列专栏: 【C语言详解】 【数据结构详解】【C++详解】 目录 1.C语言传统的处理错误的方式 2.C++异常概念 3. 异常的使用 3.1 异常的抛出和捕获 3.2 异常的重新抛出 3.3 异常安全 3.4 异常规范 4.自定义异常体系 5.C++标准库的异常体系 1.C语言传统的处理错误的方式 传统的错误处理机制:

try -catch-finally的理解,同时在try-catch-finally中含有return和throws的理解

在没有try-catch或try-catch-finally的情况下,程序正常执行到某行,在这行报错后,这行后面的代码就不执行了,程序就停止了,中断了。 例如   在有try-catch或try-catch-finally 情况上,在某行执行错误,在try中这行下的代码不执行,try外的代码执行。当然是catch在没有做处理的情况下。如果catch中做了处理,在不影响当前程序下,try

C++异常处理: try,catch,throw,finally的用法

写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使用过异常,但是你习惯使用异常了吗? 现在很多软件都是n*365*24小时运行,软件的健壮性至关重要.  内容导读本文包括2个大的异常实现概念:C++的标准异常和SEH异常. C++标

[置顶]C++异常处理:try,catch,throw,finally的用法

写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使用过异常,但是你习惯使用异常了吗? 现在很多软件都是n*365*24小时运行,软件的健壮性至关重要.  内容导读本文包括2个大的异常实现概念:C++的标准异常和SEH异常. C++标

【POJ】3660 Cow Contest floyd(可以拓扑排序?)

Cow Contest Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6925 Accepted: 3792 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating i

BFS —— POJ 3278 Catch That Cow

对应POJ题目:点击打开链接 Catch That Cow Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 54686 Accepted: 17096 Description Farmer John has been informed of the location of a fugitive cow

USACO Section 2.3 Cow Pedigrees

题意: N个节点  深度为K  的正则二叉树  求  树有几种形态 思路: 一开始以为是数学题…  看了byvoid的题解才知道是dp… 每棵树由根节点、左子树、右子树构成  由此得状态转移  树=左子树*右子树 节点数和深度是影响答案的属性  所以令dp[i][j]表示i个节点深度在j以内的树的形态数 深度在j以内的树又两个深度在j-1以内的树和一个根节点构成 设左子树k个节

ES6中try-catch

在ES6(ECMAScript 2015)中,try-catch 语句的语法和使用方式与在之前的ECMAScript版本中是一样的。try-catch 语句用于处理代码中可能发生的错误,确保程序的健壮性和用户体验。 基本语法 try { // 尝试执行的代码块 // 如果发生错误,则执行 catch 块中的代码 } catch (error) { // 处理错误 // error

【C++】try 语句捕获异常,catch子句处理异常

#include <iostream>#include <stdexcept>using namespace std;int main(){int i1, i2;while(cin >> i1 >> i2){try{if (i2 == 0)throw runtime_error("分母为0!");cout << "i1除以i2的结果是: " << i1/i2 << endl;}catch(ru

Codeforces 283. B. Cow Program记忆化搜索

output standard output Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1, a2, ..