当前位置:网站首页>Depth first search to realize the problem of catching cattle
Depth first search to realize the problem of catching cattle
2022-06-29 00:46:00 【chengqiuming】
One Link to original question
3278 -- Catch That Cow
http://poj.org/problem?id=3278
Two Input and output
1 Input
Two Numbers , The first 1 The number represents the position of the farmer , The first 2 Number represents the position of cattle .
2 Output
The minimum number of steps for a farmer to catch an ox
3、 ... and Input and output examples
1 sample input
5 17
2 sample output
4
Four Code
package graph.poj3278;
import java.util.Scanner;
public class POJ3278 {
static private int n;
static private int k;
/**
* Function description : Depth-first algorithm
*
* @param t Location of cattle
* @return Required deployment
* @author cakin
* @date 2022/6/28
* @description:
*/
static int dfs(int t) {
if (t <= n) // People step backwards
return n - t;
if (t % 2 != 0) // People are t+1 Location , Take another step to the cow's position , People are t-1 Location , Take another step to the cow's position
return Math.min(dfs(t + 1) + 1, dfs(t - 1) + 1);
else // People are t/2 The location of , Take another step to the cow's position , People directly from n Go to the t Steps
return Math.min(dfs(t / 2) + 1, t - n);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
k = scanner.nextInt();
int ans = 0;
if (n == 0) { // Judgment of special circumstances , Very important
n = 1;
ans = 1;
}
ans += dfs(k);
System.out.println(ans);
}
}5、 ... and test result
Green is the input , White is the output
![]()
边栏推荐
- The magical zero knowledge proof can not only keep secrets, but also make others believe you!
- Redis常用命令手册
- 每日一题:消失的数字
- FATAL ERROR: Could not find ./ bin/my_ print_ Solutions to defaults
- 大智慧上开户是安全的吗
- [image detection] recognition of the front and back of a coin based on texture features with matlab code attached
- Use menu resources to implement option menus and context menus
- [Gym 102423]-Elven Efficiency | 思维
- JVM底层又是如何实现synchronized的
- MySQL high availability dual master synchronization
猜你喜欢

企业和IT领导者对创新的误解

sql入门

Blazor University (34)表单 —— 获得表单状态
![[gym 102423]-elven efficiency | thinking](/img/cf/b65f3db1580a83478f8351cea22040.png)
[gym 102423]-elven efficiency | thinking

Precautions for installation and use of rotary joint

pinhole camera model
![[image detection] recognition of the front and back of a coin based on texture features with matlab code attached](/img/84/0a364adcd373cc40c9bc7b70d50f93.jpg)
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached

架构实战营|模块5

Install MySQL on Windows platform (with Navicat premium 12 "using" tutorial)
![[image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code](/img/b5/02979b50db885f0606dce455182ac4.jpg)
[image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code
随机推荐
How does the JVM bottom layer implement synchronized
11. target segmentation
Analysis of basic structure and working principle of slip ring
[MCU club] design of GSM version of range hood based on MCU [simulation design]
用户登录(记住用户)&用户注册(验证码) [运用Cookie Session技术]
HandlerThread使用及原理
var、let、const 三者的区别
Bug risk level
学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条
Blazor University (34)表单 —— 获得表单状态
[image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code
滑环的基本结构及工作原理分析
[image detection] line recognition based on Hough transform (fitting angle bisector) with matlab code
Reasons for high price of optical fiber slip ring
2022_ 2_ 16 the second day of learning C language_ Constant, variable
Encapsulation of JDBC connection and disconnection database
FATAL ERROR: Could not find ./ bin/my_ print_ Solutions to defaults
LG. Hankson's interesting questions, C language
12. object detection mask RCNN
Précautions d'installation et d'utilisation des joints rotatifs