当前位置:网站首页>[daily question 1] stone jumping -- dynamic programming
[daily question 1] stone jumping -- dynamic programming
2022-04-21 09:38:00 【Little strange corporal ~】
Algorithmic thought : The solution of this problem is that Xiao Yi should jump the slate at least several times , You can jump to the last slate . Then it is to solve the optimal solution of a stone jumping board , Then we will think of using dynamic programming to solve . In a nutshell Dynamic programming is the result of your solution at this step , Use the result of your last step .
Because in this question, you need to input the position of the stone slab where Xiaoyi is now , And the location of the stone slab that Xiaoyi will eventually reach . We define the original slate position as n, The final location is defined as m, Create a new array step, initialization step The content in is the maximum value of the shaping . So Xiaoyi's current position is step[n], Because Xiaoyi is going to the n The number of steps required for a slate is now 0, therefore step[n] = 0; stay step The array represents , What Xiaoyi wants step Subscript steps . The number of the next slate = The current slate number + Now a divisor of the slate number .

//N = 4,M = 24:
//4->6->8->12->18->24
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();// Xiaoyi's initial position
int m = scanner.nextInt();// Where Xiaoyi will finally reach
int []step = new int[m + 1];
// Create an array step, stay step Array , It stores the number of stone jumping boards , Here we initialize to the maximum value of an integer
for(int i = 0;i<step.length;i++){
step[i] = Integer.MAX_VALUE;
}
step[n] = 0; // Xiao Yi in the first n A slate , To the first n A slate doesn't need to jump , So set step[n] =
for(int i = n;i<step.length;i++){
if(step[i] == Integer.MAX_VALUE){
continue;
}
List<Integer> list = div(i); // Get divisor
for(int j : list){
if(i + j <= m && step[i + j] != Integer.MAX_VALUE){
step[i+j] = Math.min(step[i+j],step[i] + 1); // Because there are several ways to jump on a slate , Here, in order to get the optimal solution
// So get the minimum number of steps to achieve the stone slab
}else if(i + j <= m){
// The next slate to jump to = The current position of the slate + Now a divisor of the number of stone slabs , After finding the corresponding slate , Number of steps on the previous slate + 1
step[i+j] = step[i] + 1;
}
}
}
if(step[m] == Integer.MAX_VALUE){
System.out.println(-1);
}else{
System.out.println(step[m]);
}
}
public static List<Integer> div(int n){
List<Integer> list = new ArrayList<>();
for(int i = 2;i * i <= n;i++){
if(n % i == 0){
list.add(i);
if(n / i != i){
list.add(n / i);
}
}
}
return list;
}
版权声明
本文为[Little strange corporal ~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210926240173.html
边栏推荐
- Fashion cloud learning -js implementation disables right click and F12
- ESP32 寻迹模块测试
- Transaction isolation level and mvcc
- 1147: find subarray
- 1170: 最长字符串(指针专题)
- 【Flutter 专题】124 日常问题小结 (三) 自定义 Dialog 二三事
- What are the products of Guangzhou futures exchange?
- Transaction isolation level and mvcc
- 1150: how many integers
- 【手拉手 带你准备电赛】April Tag标记跟踪(3D定位)详解
猜你喜欢

比 Navicat 还要好用、功能更强大的工具!

【栈和队列】C语言简单应用 ⌊栈和队列互相实现,循环队列⌉

Fashion cloud learning -js implementation disables right click and F12

The display problem of gltf model with transparent map

C语言进阶-动态内存管理

You are using pip version 20.2.3; however, version 22.0.4 is available. You should consider

Download the first analysis report on China's database industry!
![Kali:sqlmap :[10:39:37] [CRITICAL] unable to connect to the target URL](/img/bf/123e6f5eadb8d502e135a7cff9b120.png)
Kali:sqlmap :[10:39:37] [CRITICAL] unable to connect to the target URL

Esp32 tracing module test

使用LamdbaUpdateWrapper的setSql作用及风险
随机推荐
【笔记】CMakeLists.txt 文件语法记录
使用LamdbaUpdateWrapper的setSql作用及风险
多线程---解析无锁队列的原理与实现
1155: multiple instances of string
Transaction isolation level and mvcc
【概率论与数理统计】1.4 条件概率
Promise处理复杂异步
redis部署与使用
[Yugong series] April 2022 wechat applet - Project (bus query) - 01 surrounding stations
ESP32 寻迹模块测试
The display problem of gltf model with transparent map
You are using pip version 20.2.3; however, version 22.0.4 is available. You should consider
Control the start, hide, display and close of another program
组合总和-Leetcode
1152: binary search
1150: 数数多少个整数
vr全景适合那些行业
元术练舞室,赛博朋克酷炫风格等你来畅跳
浅谈数据库设计之三大范式
Geek challenge 2019 upload 1
