当前位置:网站首页>【每日一题】跳石板--动态规划
【每日一题】跳石板--动态规划
2022-04-21 09:26:00 【小小怪下士~】
算法思想:本题求解的是小易要最少跳几次石板,就可以跳到最后一个石板。那么就是求解一个跳石板的最优解,那么我们就会联想到使用动态规划来进行求解。简单的来说动态规划就是你这一步要求解的结果,要利用上你上一步求解的结果.
因为在本题中要输入现在小易所在的石板位置,和最终小易要到达的石板位置。我们把起先的石板位置定义为n,最终的位置定义为m,新建一个数组step,初始化step中的内容为整形的最大值。那么小易现在的位置就是step[n],因为小易要到第n个石板现在所需的步数为0,所以step[n] = 0;在step数组中表示的是,小易要到的对应step下标的步数。下一个石板的编号 = 现在的石板编号 + 现在石板编号的一个约数。

//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();//小易的初始位置
int m = scanner.nextInt();//小易最终要到达的位置
int []step = new int[m + 1];
//创建出一个数组step,在step数组,里面存放的是跳石板的次数,在这里我们初始化为整数的最大值
for(int i = 0;i<step.length;i++){
step[i] = Integer.MAX_VALUE;
}
step[n] = 0; //小易在第n个石板,到第n个石板不需要跳,所以设置step[n] =
for(int i = n;i<step.length;i++){
if(step[i] == Integer.MAX_VALUE){
continue;
}
List<Integer> list = div(i); //得到约数
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); //因为跳到一个石板上有好几种跳法,在这里为了得到最优解
//所以得到做到石板的最小步数
}else if(i + j <= m){
//下一个要跳到的石板=现在的石板位置+现在石板个数的一个约数,找到相应的石板之后,步数在上一个石板上的步数 + 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;
}
版权声明
本文为[小小怪下士~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_54883034/article/details/124261852
边栏推荐
- C语言进阶-动态内存管理
- 计算器(力扣)
- Error in idea connection to sqlserver
- 1168: Bill (pointer topic)
- Performance analysis ideas
- Pipy mqtt agent (III) logging
- Pipy MQTT 代理之(三)Logging
- 1148: combine one of three digits
- Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality Artistic Style Transfer--T Li
- Actual combat penetration - fofa dirbrute - code audit - construction POC ueditor - decryption - WAF Godzilla
猜你喜欢
Open3d reads and writes ply point cloud files

CC00043.CloudJenkins—————————————

原生与H5混合式开发详解

Getting started with object detection FAQs (deep learning / image classification)

极客大挑战 2019 Upload 1
![Buuctf [actf2020 freshman competition] include](/img/20/3c0d0286c869385ee0c9a119e337e2.png)
Buuctf [actf2020 freshman competition] include

【CVPR 2020】PointASNL :Robust Point Clouds Processing using Nonlocal Neural Networks
![[(strongly pushed) Li Hongyi 2021 / 2022 spring machine learning course] unsupervised learning - linear methods](/img/2a/de77caa5516ed34af52c14bbe8dbc7.png)
[(strongly pushed) Li Hongyi 2021 / 2022 spring machine learning course] unsupervised learning - linear methods

【ACM】131. 分割回文串

Yapi basic use (2022-04-15)
随机推荐
1157: 连续的n个1
YApi基本使用(2022-04-15)
事务的隔离级别与MVCC
1153: 简易版最长序列
Dark blue - Visual slam - Section 6 exercise
C语言进阶-动态内存管理
云网融合 — 算力中心 — RoCE/RMDA 与 NVMe/NVMe-oF
1151: 大整数加法
Note 0104 MySQL advanced - index - Overview
CC00026. CloudJenkins—————————————
In 2017, I also started to write CSDN blog (Sina Netease moved to CSDN)
Multithreaded copy set (new edition 3)
1170: longest string (pointer)
1163: affinity string (string)
【CVPR 2020】PointASNL :Robust Point Clouds Processing using Nonlocal Neural Networks
Download the first analysis report on China's database industry!
1164: string encryption
Penetration practice - dig a school site vulnerability (APP vulnerability)
Detailed explanation of kotlin cooperation process lanch
1161: string length (pointer)
