anotherOne

This commit is contained in:
dadgam3er 2024-09-13 17:21:57 -04:00
parent 2384eeaf7e
commit 8c5ac774d2
2 changed files with 40 additions and 0 deletions

BIN
OneDArray.class Normal file

Binary file not shown.

40
OneDArray.java Normal file
View File

@ -0,0 +1,40 @@
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();
}
}