You are viewing a single comment's thread. Return to all comments →
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] game) { // Return true if you can win the game; otherwise, return false. int n=game.length; boolean []visited = new boolean[game.length]; Queue<Integer> quee = new LinkedList<Integer>(); quee.add(0); while(quee.size() !=0){ int i=quee.poll(); if(!visited[i]){ if(i+leap>=n || i + 1 >= n) { return true; } else { visited[i]=true; if(i+leap<n){ if(game[i+leap]==0){ quee.add(i+leap); } } if(game[i+1]==0){ quee.add(i+1); } if(i>0){if(game[i-1]==0){ quee.add(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(); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Java 1D Array (Part 2)
You are viewing a single comment's thread. Return to all comments →
import java.util.*;
public class Solution {
}