첫 번째 요소에서 시작하여 첫 번째 요소에서 도달할 수 있는 모든 요소를 반복적으로 호출할 수 있습니다. 처음에서 끝까지 도달하기 위한 최소 점프 수는 처음에서 도달할 수 있는 요소에서 끝까지 도달하는 데 필요한 최소 점프 수를 사용하여 계산할 수 있습니다.
배열 =={1, 3, 6, 3, 2, 3, 6, 8, 9, 5};
필요한 단계 수는 4입니다.
예
using System;
namespace ConsoleApplication{
public class Arrays{
public int MinJumps(int[] arr, int l, int h){
if (h == l)
return 0;
if (arr[l] == 0)
return int.MaxValue;
int min = int.MaxValue;
for (int i = l + 1; i <= h && i <= l + arr[l]; i++){
int jumps = MinJumps(arr, i, h);
if (jumps != int.MaxValue && jumps + 1 < min)
min = jumps + 1;
}
return min;
}
}
class Program{
static void Main(string[] args){
Arrays a = new Arrays();
int[] arrm = { 1, 3, 6, 3, 2, 3, 6, 8, 9, 5 };
int n = arrm.Length;
Console.Write(" Minimum number of jumps to reach end is " + a.MinJumps(arrm, 0, n - 1));
}
}
} 출력
4