-
Notifications
You must be signed in to change notification settings - Fork 0
/
day54.java
36 lines (31 loc) · 1.02 KB
/
day54.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
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] game) {
Queue<Integer> queue = new LinkedList<>();
queue.add(0);
while(!queue.isEmpty()) {
int curr = queue.poll();
if (curr < 0 || game[curr] == 1) continue;
if (curr + leap >= game.length || curr + 1 >= game.length) return true;
game[curr] = 1;
queue.add(curr - 1);
queue.add(curr + 1);
queue.add(curr + leap);
}
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();
}
}