codeChallenge/OneDArray.java
2024-09-13 17:23:54 -04:00

41 lines
1.1 KiB
Java

import java.util.Scanner;
public class OneDArray {
public static boolean canWin(int leap, int[] game, int index) {
// Base cases
if (index < 0 || game[index] == 1) {
return false;
}
if (index + 1 >= game.length || index + leap >= game.length) {
return true;
}
// Mark current position as visited
game[index] = 1;
// Recursive cases
return canWin(leap, game, index - 1) || // Move backward
canWin(leap, game, index + 1) || // Move forward
canWin(leap, game, index + leap); // Jump forward
}
public static boolean canWin(int leap, int[] game) {
return canWin(leap, game, 0); // Start from index 0
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
while (q-- > 0) {
int n = scan.nextInt();
int leap = scan.nextInt();
int[] game = new int[n];
for (int i = 0; i < n; i++) {
game[i] = scan.nextInt();
}
System.out.println((canWin(leap, game)) ? "YES" : "NO");
}
scan.close();
}
}