-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java_1D_Array_Part_2.java
41 lines (32 loc) · 1.05 KB
/
Java_1D_Array_Part_2.java
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
37
38
39
40
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] game) {
return checkSolution(leap, game, 0);
}
public static boolean checkSolution(int leap, int[] game, int i) {
if (i < 0 || game[i] == 1) {
return false;
}
else if (i + 1 >= game.length || i + leap >= game.length) {
return true;
}
game[i] = 1;
return checkSolution(leap, game, i + leap) ||
checkSolution(leap, game, i + 1) ||
checkSolution(leap, game, i - 1);
}
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();
}
}