当前位置:网站首页>Solve the kangaroo crossing problem (DP)
Solve the kangaroo crossing problem (DP)
2022-07-28 20:14:00 【Tiredd】
Problem description : A kangaroo wants to jump from one side of the river to the other , The river is wide , But there are many piles in the middle of the river , There is one every other meter , There is a spring on each pile , Kangaroo can jump farther by jumping on the spring . Each spring has a different force , Use a number to represent its power , If the force of the spring is 5, It means that kangaroos jump the most next Can jump 5 rice , If 0, It means that you will fall into it and can't continue to jump . The river has a total of n Meters wide , Kangaroo is initially on the first spring , If you jump to the last spring, you'll cross the river , Given the force of each spring , How many jumps does the kangaroo need at least to reach the opposite bank . If you can't get to , Output one 1.
Input description : The input is divided into two lines , The first 1 The row is the length of the array n(1≤n≤10000), The first 2 Line is every - Item value , Separate... With spaces .
Output description : Output the least hops , If output one cannot be reached 1.
sample input :
5
2 0 1 1 1
sample output :
4
Ideas : A classic linear dp problem , You can define States f[i] For from 1 Go to the i The least jump , Then the state transition equation can be obtained as f[i] = min(f[i], f[j] + 1) (j < i && j + h[j] >= i), The answer for f[n + 1].
The code is as follows :
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10010;
int n, h[N], f[N];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);
memset(f, 0x3f3f3f3f, sizeof f);
f[1] = 0;
for(int i = 2; i <= n + 1; i ++ )
for(int j = 1; j < i; j ++ )
if(j + h[j] >= i)
f[i] = min(f[i], f[j] + 1);
if(f[n + 1] == 0x3f3f3f3f) puts("-1");
else printf("%d", f[n + 1]);
return 0;
}You can find , The complexity of the defined state transition equation is O(n^2), But another state transition equation can also be defined .f[i] For from i The least jump to the opposite bank , So the state transfer equation is f[i] = min(f[i], f[i + j] + 1]) (1 <= j <= h[i]), So the answer is f[1], And the time complexity is O(n + sum) (sum = h[1] + h[2] + …………h[n]), So we can choose the method with less time complexity according to the data range given by the topic .
The code is as follows :
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10010;
int n, h[N], f[N];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);
for(int i = n; i >= 1; i -- )
{
f[i] = 1 << 30;
if(h[i] != 0)
for(int j = 1; j <= h[i]; j ++ )
f[i] = min(f[i], f[i + j] + 1);
}
if(f[1] == 1 << 30) puts("impossible");
else printf("%d", f[1]);
return 0;
}
/*
14
2 1 2 3 0 0 5 5 1 1 1 1 13 14
*/边栏推荐
- [C language] step jumping problem [recursion]
- Item exception handling in SSM
- [C language] random number generation and `include < time. H > 'learning
- C+ + core programming
- [C language] initial C language reflection and summary
- C language - control statement
- A chip company fell in round B
- Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
- 为什么客户支持对SaaS公司很重要?
- 7. Functions of C language, function definitions and the order of function calls, how to declare functions, prime examples, formal parameters and arguments, and how to write a function well
猜你喜欢

The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!

Idea properties file display \u solution of not displaying Chinese

软考中级(系统集成项目管理工程师)高频考点

JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs

Edge detection and connection of image segmentation realized by MATLAB

数字滤波器设计——Matlab

Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich

Advanced notes (Part 2)

2022年下半年系统集成项目管理工程师认证8月20日开班

3、 Are formal and actual parameters in a programming language variables?
随机推荐
Why is there no log output in the telnet login interface?
MySQL命令语句(个人总结)
C language pointer and two-dimensional array
XOR operation and its usage
党员故事|李青艾用漫画带动农民增收致富
[C language] header file of complex number four operations and complex number operations
跨区域网络的通信学习静态路由
4. Const and difine and the problem of initializing arrays with const and define
New fruit naming (DP is similar to the longest common subsequence)
[C language] simulation implementation of pow function (recursion)
最大交换[贪心思想&单调栈实现]
Tencent cloud deployment lamp_ Experience of building a station
83.(cesium之家)cesium示例如何运行
C language operators and input and output
Const pointer of C language and parameter passing of main function
2. Floating point number, the difference between float and double in C language and how to choose them
[experiment sharing] CCIE BGP reflector experiment
数字图像理论知识(一)(个人浅析)
C language - data storage
How to use pycharm to quickly create a flask project