欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[java]跳跃最大长度

程序员文章站 2022-06-12 08:53:55
...
import java.util.Scanner;

public class T52 {
	public static void printColor (int mapArray[], int arr[], int currPos) {
		int minIndex = currPos - arr[currPos] >= 0 ? currPos - arr[currPos] : 0;
		int maxIndex = currPos + arr[currPos] <= arr.length - 1 ? currPos + arr[currPos] : arr.length - 1;
		for (int i = minIndex ; i <= maxIndex ; i++) {
			if(mapArray[i] == 0) {
				mapArray[i] = 1;
				printColor(mapArray, arr, i);
			}
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n;
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		int arr[] = new int[n];
		int mapArray[] = new int[n];
		int min = 0, max = 0;
		for (int i = 0 ; i < n ; i++) {
			arr[i] = sc.nextInt();
			mapArray[i] = 0;
		}
		printColor(mapArray, arr, 0);
		if (mapArray[n-1] == 1) {
			System.out.print("true");
		}
		else {
			System.out.print("false");
		}
	}

}

[java]跳跃最大长度

使用了染色+递归解决问题