-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJava1DArrayPart2.java
More file actions
42 lines (37 loc) Β· 975 Bytes
/
Java1DArrayPart2.java
File metadata and controls
42 lines (37 loc) Β· 975 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
37
38
39
40
41
42
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Java1DArrayPart2 {
public static boolean canWin(int leap, int[] game) {
int i = 0;
Queue<Integer> list = new LinkedList<>();
list.add(leap);
list.add(i + 1);
while (!list.isEmpty()) {
int next = list.poll();
if (next > game.length - 1) return true;
if (game[next] == 0) {
i = next;
if (i != 0) list.add(i - 1);
list.add(i + leap);
list.add(i + 1);
game[i] = 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();
}
}