2017-02-16 23:51:56 -06:00
|
|
|
// Author: Rodney Shaghoulian
|
2018-07-27 16:45:56 -07:00
|
|
|
// Github: github.com/RodneyShag
|
|
|
|
|
// HackerRank: hackerrank.com/RodneyShag
|
2017-02-16 23:51:56 -06:00
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
|
|
public class Solution {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Scanner scan = new Scanner(System.in);
|
|
|
|
|
int T = scan.nextInt();
|
2017-04-05 18:42:35 -05:00
|
|
|
while (T-- > 0) {
|
2017-02-16 23:51:56 -06:00
|
|
|
int n = scan.nextInt();
|
|
|
|
|
int m = scan.nextInt();
|
|
|
|
|
int [] array = new int[n];
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
array[i] = scan.nextInt();
|
|
|
|
|
}
|
2017-04-05 18:42:35 -05:00
|
|
|
System.out.println(isSolvable(array, m, 0) ? "YES" : "NO");
|
2017-02-16 23:51:56 -06:00
|
|
|
}
|
2017-04-05 18:42:35 -05:00
|
|
|
scan.close();
|
2017-02-16 23:51:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Basically a depth-first search (DFS).
|
|
|
|
|
Marks each array value as 1 when visiting (Side-effect: alters array) */
|
2017-06-10 23:02:02 -07:00
|
|
|
private static boolean isSolvable(int[] array, int m, int i) {
|
2017-02-16 23:51:56 -06:00
|
|
|
/* Base Cases */
|
|
|
|
|
if (i < 0 || array[i] == 1) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (i + 1 >= array.length || i + m >= array.length) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
array[i] = 1; // marks as visited
|
|
|
|
|
|
2017-06-10 23:02:02 -07:00
|
|
|
/* Recursive Cases (Tries +m first to try to finish game quickly) */
|
|
|
|
|
return isSolvable(array, m, i + m) ||
|
2017-04-05 18:42:35 -05:00
|
|
|
isSolvable(array, m, i + 1) ||
|
2017-06-10 23:02:02 -07:00
|
|
|
isSolvable(array, m, i - 1);
|
2017-02-16 23:51:56 -06:00
|
|
|
}
|
|
|
|
|
}
|