I have the following task, but I don’t understand how I could solve it through dynamic programming or perhaps I need to solve it through graphs, could someone help me solve it?
The frog Sandy likes to jump on stones in a pond. In her pond, there are n stones, numbered from 1 to n. The stones are small, so we consider them as points. The stones i are located at coordinates (x i,yi). Today, Sandy jumped from stone 1 to stone n. Along the way, she could revisit some stones (including stones 1 and n) multiple times. Sandy can make jumps of any length. However, jumping tires her, so each of her jumps, except the first one, is strictly shorter than the immediately preceding one.
Task
Given the positions of the stones, calculate the maximum number of jumps Sandy could make while traveling from stone 1 to stone n.
Input and Output Format
The first line of input contains the number of stones ????. In the i-th of the following n lines are the coordinates of stone number i.
Constraints and Evaluation
There are 10 test sets, and you can earn 1 point for each set. In the individual sets, the maximum value of n is as follows: 2, 3, 7, 18, 50, 100, 200, 1000, 2000, 3000. All coordinates are within the range from 0 to 10^9, inclusive. In the first five test sets, no coordinate exceeds 10^4.
Example
Input:
6
0 1
1 5
1 0
3 3
5 5
3 2
Output:
7
Note:
One optimal sequence of 7 jumps:
1 -> 3 -> 5 -> 4 -> 6 -> 5 -> 4 -> 6
These jumps have the following lengths in order:
2√17 -> √29 -> 5 -> √10 -> 3 -> 3 -> 2 -> 1
I’ve tried thid code, but it doesn’t work, because I don’t understand what condition should be used in dynamic programming?
import math
def distance(p1, p2):
return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
def max_jumps(stones):
n = len(stones)
if n == 1:
return 0
dp = [1] * n
for i in range(1, n):
for j in range(i):
if dp[j] > 0 and (i == 1 or distance(stones[j], stones[i]) < distance(stones[j], stones[j - 1])):
dp[i] = max(dp[i], dp[j] + 1)
return dp[-1]
n = int(input())
stones = []
for _ in range(n):
x, y = map(int, input().split())
stones.append((x, y))
result = max_jumps(stones)
print(result)