Problem Statement
There is a frog trying to cross a lake. The lake contains N stones with a number written
on each of them. It is given that arr; denotes the number on the ith stone.
It is guaranteed that any point after the last stone is considered to be outside the lake.
Also, whenever a frog is on a stone his jump length will become at most equal to the
number written on that stone
For example: If the frog is on the stone number 2 and arr[2] is 3, the frog can jump to
any stone from 3 to 5.
Find out the minimum number of jumps needed for the frog to reach out of the lake.
Note: It is given that the frog starts jumping from the first stone.
Input Format
The first line contains an integer, N, denoting the number of elements in arr.
Each line of the N subsequent lines (where 0 Si< N) contains an integer describing program in python
Answers
Answered by
0
Program:
def minimum_jumps(arr, lower, higher):
if higher == lower:
return 0
if arr[lower] == 0:
return float('inf')
minimum = float('inf')
for i in range(lower + 1, higher + 1):
if (i < lower + arr[lower] +1):
jumps = minimum_jumps(arr, i, higher)
if (jumps != float('inf') and jumps + 1 < minimum):
minimum = jumps + 1
return minimum
N = int(input("Enter total number of stones : "))
arr = []
print("Enter number written on the stones :")
for i in range(0, N):
item = int(input())
arr.append(item)
print("Minimum number of jumps required : ", minimum_jumps(arr, 0, N))
Similar questions