当前位置:首页|资讯

GFG 169 Jumping Game

作者:您是打尖儿还是住店呢发布时间:2024-10-16

Geek 正在一个有平台的未来竞技场中玩一个冒险游戏。竞技场中有 N 个平台,编号从 1 到 N(使用以 1 为基础的索引)。

每个平台都有一个跳板,可以让 Geek 跳到其他平台。他可以从平台 i 跳到平台 (i + d ) 或平台 (i - d ),其中 d 是每个平台的特殊距离。但 Geek 必须待在竞技场内,所以他不能落在平台 1 之前或平台 N 之后。


每次跳跃都需要消耗能量,从平台 i 跳下所需的能量为 c 。您的任务是找出 Geek 从每个平台(1 ≤ i ≤ N)到达 N 平台所需的最小能量。您需要返回一个数组,其中包含每个平台到达终点所需的最小能量。


注意:如果不可能从某个平台到达 N 平台,则该平台的返回值为-1。



Geek is playing an adventurous game in a futuristic arena with platforms. There are N platforms in the arena, numbered from 1 to N (using 1-based indexing).

Each platform has a Jump Pad that lets Geek jump to other platforms. From platform i, he can jump to platform (i + d ) or platform (i - d ), where d is a special distance for each platform. But Geek has to stay within the arena, so he can't land before platform 1 or after platform N.


Every jump costs energy, and the energy needed to jump from platform i is c . Your task is to  find out the minimum energy Geek must spend to reach platform N from each platform (1 ≤ i ≤ N). You need to return an array with the minimum energy required for each platform to reach the end.


NOTE: If it's impossible to reach platform N from a certain platform, return is -1 for that platform.


Example 1:


Input:

n = 4

d = [1, 1, 1, 1]

c = [10, 5, 2, 4]

Output: [17, 7, 2, 0]

Explanation: 

Platform 4: Minimum energy to reach itself is 0.

Platform 3: Minimum energy to reach platform 4 is 2 (3 -> 4).

Platform 2: Minimum energy to reach platform 4 is 5 (2 -> 3) + 2 (3 -> 4) = 7.

Platform 1: Minimum energy to reach platform 4 is 10 (1 -> 2) + 5 (2 -> 3) + 2 (3 -> 4) = 17.



Your Task:

You don't need to read input or print anything. Your task is to complete the

function jumpGame() which takes one integer N and two arrays d and c as parameters. The

function should return the array containing minimum energy geek required for each platform to reach the end.

Constraints:

1 ≤ N ≤ 10

1 ≤ d[i] ≤ N

1 ≤ c[i] ≤ 10

--------

建图,然后根据dijkstra算法求出每个位置的最短路,就是不理解的是为什么用bfs不行呢?

这个dijkstra算法还不太熟悉,得再练习下才行的。



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1