-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.java
48 lines (38 loc) · 1.03 KB
/
Solution.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
41
42
43
44
45
46
47
48
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] game) {
Queue<Integer> stack = new LinkedList<>();
stack.add(0);
while (!stack.isEmpty()) {
int current = stack.poll();
game[current] = 2;
if (current + 1 >= game.length || current + leap >= game.length) {
return true;
}
if (game[current + 1] == 0) {
stack.add(current + 1);
}
if (game[current + leap] == 0) {
stack.add(current + leap);
}
if (current - 1 >= 0 && game[current - 1] == 0) {
stack.add(current - 1);
}
}
return false;
}
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();
}
}