-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (30 loc) · 1021 Bytes
/
Solution.java
File metadata and controls
36 lines (30 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] array, int currentLocation) {
if (currentLocation >= array.length) {
return true;
}
if (currentLocation < 0 || array[currentLocation] == 1) {
return false;
}
// mark current location as visited
array[currentLocation] = 1;
return canWin(leap, array, currentLocation - 1) ||
canWin(leap, array, currentLocation + 1) ||
canWin(leap, array, currentLocation + leap);
}
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, 0) ? "YES" : "NO");
}
scan.close();
}
}