我有一个关于递归的小问题。下面的代码实际上是对到达结束的最小跳转次数问题的答案
// C program to find Minimum
// number of jumps to reach end
#include <limits.h>
#include <stdio.h>
// Returns minimum number of
// jumps to reach arr[h] from arr[l]
int minJumps(int arr[], int l, int h)
{
// Base case: when source and destination are same
if (h == l)
return 0;
// When nothing is reachable from the given source
if (arr[l] == 0)
return INT_MAX;
// Traverse through all the points
// reachable from arr[l]. Recursively
// get the minimum number of jumps
// needed to reach arr[h] from these
// reachable points.
int min = INT_MAX;
for (int i = l + 1; i <= h && i <= l + arr[l]; i++) {
int jumps = minJumps(arr, i, h);
if (jumps != INT_MAX && jumps + 1 < min)
min = jumps + 1;
}
return min;
}
// Driver program to test above function
int main()
{
int arr[] = { 1, 3, 6, 3, 2, 3, 6, 8, 9, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
printf(
"Minimum number of jumps to reach end is %d ",
minJumps(arr, 0, n - 1));
return 0;
}
当h==l和arr[l]==0时,函数返回 sth 并且函数结束。否则,它会更新一个名为jumps的变量,但我无法理解该语句。例如,当 i=1 或 i=2 时,jumps 的值是多少等等。换句话说,我无法理解 jumps 变量更新过程的意义。