Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Solution {
- public static boolean canWin(int leap, int[] game) {
- int pos = 0;
- while (pos < game.length) {
- if (pos >= game.length || pos + leap >= game.length)
- return true;
- if (canMoveOneForward(pos, game)) {
- pos++;
- continue;
- }
- if (canLeap(leap, pos, game)) {
- pos += leap;
- continue;
- }
- if(canBack(pos, game)){
- while (canBack(pos, game)) {
- game[pos] = 1;
- pos--;
- if (canLeap(leap, pos, game)) {
- pos += leap;
- break;
- }
- }
- continue;
- }
- return false;
- }
- return false;
- }
- public static boolean canMoveOneForward(int pos, int[] game){
- if (pos+1 < game.length)
- return game[pos+1] == 0;
- return false;
- }
- public static boolean canLeap(int leap, int pos, int[] game) {
- if (game[pos + leap] == 0)
- return true;
- return false;
- }
- public static boolean canBack(int pos, int[] game) {
- if (pos > 0)
- if (game[pos - 1] == 0)
- return true;
- 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();
- }
- }
Add Comment
Please, Sign In to add comment