Skip to content

Instantly share code, notes, and snippets.

@moazmohamed20
Last active May 7, 2022 22:59
Show Gist options
  • Save moazmohamed20/dfe4321601bd8000c24959ee66efc9d7 to your computer and use it in GitHub Desktop.
Save moazmohamed20/dfe4321601bd8000c24959ee66efc9d7 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] game) {
return canWinFrom(0, leap, game);
}
public static boolean canWinFrom(int i, int leap, int[] game) {
// If: 0 < i <= game.length
if (i >= game.length) return true; // If: i Passed The Whole Array
else if (i < 0 || game[i] != 0) return false; // If: i Out Of Arry || i Steped On 1
game[i] = 1; // Marks As Visited
return canWinFrom(i+leap, leap, game) || canWinFrom(i+1, leap, game) || canWinFrom(i-1, leap, game);
}
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment