anotherOne
This commit is contained in:
parent
2384eeaf7e
commit
8c5ac774d2
BIN
OneDArray.class
Normal file
BIN
OneDArray.class
Normal file
Binary file not shown.
40
OneDArray.java
Normal file
40
OneDArray.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user