当前位置:网站首页>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
![]()
边栏推荐
- Excel使用过程中的参考资料
- 成功解决(机器学习分割数据问题):ModuleNotFoundError: No module named ‘sklearn.cross_validation‘
- 个人买同业存单基金选择什么证券公司开户好,更安全
- 每日一题:数组中数字出现的次数2
- Nodejs installation and download
- How the slip ring motor works
- Two ways to set up a single Nacos load balancing ribbon polling policy weight
- 【leetcode】153. Find the lowest value in the rotation sort array
- [leetcode] 522. Longest special sequence II violence + double pointer
- Install MySQL on Windows platform (with Navicat premium 12 "using" tutorial)
猜你喜欢
![[MCU club] design of GSM version of range hood based on MCU [physical design]](/img/cf/65c1bdbb45afcc6db265a79c25a3ae.jpg)
[MCU club] design of GSM version of range hood based on MCU [physical design]

oracle 去掉html标签

12. object detection mask RCNN

Structure of the actual combat battalion | module 5

Difference between applying for trademark in the name of individual and company
JVM底层又是如何实现synchronized的

Précautions d'installation et d'utilisation des joints rotatifs

Reprint: VTK notes - clipping and segmentation - 3D curve or geometric cutting volume data (black mountain old demon)

Reference materials in the process of using Excel

机器视觉系统的配件及工作过程
随机推荐
利用verilogA模块采样
Introduction to four MySQL engines
Single machine multi instance MySQL master-slave replication
[MCU club] design of GSM version of range hood based on MCU [physical design]
12. Détection d'objets Mask rcnn
What is redis
滑环电机是如何工作的
Reprint: VTK notes - clipping and segmentation - 3D curve or geometric cutting volume data (black mountain old demon)
[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)
盘点 6 月 yyds 的开源项目!
浏览器缓存库设计总结(localStorage/indexedDB)
基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
[staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)
Précautions d'installation et d'utilisation des joints rotatifs
[MCU club] design of GSM version of range hood based on MCU [simulation design]
Haskell 配置 VS code 开发环境 (2022年6月)
[Gym 102423]-Elven Efficiency | 思维
Matrix compression
If you can play these four we media operation tools, the sideline income of 6000+ is very easy
Redis common command manual