当前位置:网站首页>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
*/边栏推荐
- Getting started with enterprise distributed crawler framework
- ssm中项目异常处理
- [C language] simulation implementation of strlen (recursive and non recursive)
- 云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- C language function
- The privatized instant messaging platform protects the security of enterprise mobile business
- [C language] random number generation and `include < time. H > 'learning
- 河北:稳粮扩豆助力粮油生产提质增效
- Common modules of saltstack
猜你喜欢

中国能否在元宇宙的未来发展中取得突破,占领高地?

In the second half of 2022, the system integration project management engineer certification starts on August 20

熊市下PLATO如何通过Elephant Swap,获得溢价收益?

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

How to automatically store email attachments in SharePoint

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation

Digital filter design matlab

Advanced notes (Part 2)

3、 Are formal and actual parameters in a programming language variables?

9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
随机推荐
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
WFST decoding process
ssm中项目异常处理
Read how to deploy highly available k3s with external database
Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
How to automatically store email attachments in SharePoint
为什么客户支持对SaaS公司很重要?
[C language] print pattern summary
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
4. Const and difine and the problem of initializing arrays with const and define
C language - control statement
私有化部署的即时通讯平台,为企业移动业务安全保驾护航
[C language] string reverse order implementation (recursion and iteration)
[C language] Pointer elementary knowledge points
Intermediate soft test (system integration project management engineer) high frequency test site
Labelme (I)
English translation Italian - batch English translation Italian tools free of charge
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Function fitting based on MATLAB
flask_ Mail source code error