当前位置:网站首页>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
![]()
边栏推荐
- 毕业三年的25岁码农总结
- 养老年金险是理财产品吗?预期收益在哪看?
- [MCU club] design of classroom number detection based on MCU [physical design]
- 【架构师(第三十八篇)】 服务端开发之本地安装最新版 MySQL 数据库
- 【leetcode】17. Letter combination of telephone number
- 每日一练:删除有序数组中的重复项
- Two fresh students: one is practical and likes to work overtime, and the other is skilled. How to choose??
- Blazor University (34)表单 —— 获得表单状态
- Bug risk level
- 运营级智慧校园系统源码 智慧校园小程序源码+电子班牌+人脸识别系统
猜你喜欢

Program environment and pretreatment

Realization of beauty system with MATLAB

Matrix compression

10. Yolo series
![用户登录(记住用户)&用户注册(验证码) [运用Cookie Session技术]](/img/31/c84c1e15aa1c73814c4ad643e3dd36.png)
用户登录(记住用户)&用户注册(验证码) [运用Cookie Session技术]

同期群分析是什么?教你用 SQL 来搞定

每日一题:数组中数字出现的次数2

pinhole camera model

12. object detection mask RCNN

Report on the convenient bee Lantern Festival: the first peak sales of pasta products this year; prefabricated wine dumplings became the winners
随机推荐
10. Yolo series
How to calculate the income tax of foreign-funded enterprises
个人买同业存单基金选择什么证券公司开户好,更安全
Go1.18 new feature: discard strings Title Method, a new pit!
What is redis
JVM底层又是如何实现synchronized的
12.物體檢測Mask-Rcnn
Daily question 1: the number of numbers in the array
Haskell configuring vs code development environment (june2022)
SCP copy folder
成功解决(机器学习分割数据问题):ModuleNotFoundError: No module named ‘sklearn.cross_validation‘
[MCU club] design of classroom number detection based on MCU [simulation design]
FSS object storage how to access the Intranet
Use and principle of handlerthread
ES6:let、const、箭头函数
cocoscreator动态切换SkeletonData实现骨骼更新
Reprint: VTK notes - clipping and segmentation - 3D curve or geometric cutting volume data (black mountain old demon)
启牛学堂帮忙开证券账户是安全靠谱的吗?个人如何开
Click hijack: X-FRAME-OPTIONS is not configured
[image detection] line recognition based on Hough transform (fitting angle bisector) with matlab code