You are viewing a single comment's thread. Return to all comments →
class Rules { public static boolean hasZero(int value) {return String.valueOf(value).contains("0");} public static boolean finish(int idx, int leap, int[] game) {return (idx + 1 > game.length - 1 || idx + leap > game.length - 1);} public static boolean goBack(int idx, int[] game) {return (idx != 0 && hasZero(game[idx - 1]));} public static boolean goForward(int idx, int[] game) {return hasZero(game[idx+1]);} public static boolean leap(int idx, int leap, int[] game) {return hasZero(game[idx+leap]);} } public class Solution { public static boolean canWin(int idx,int leap, int[] game) { if (Rules.finish(idx, leap, game)) return true; game[idx] = 1; boolean canWin = Rules.goForward(idx, game) && canWin(idx + 1, leap, game) || Rules.leap(idx, leap, game) && canWin(idx + leap, leap, game) || Rules.goBack(idx, game) && canWin(idx - 1, leap, game); game[idx] = 0; return canWin; } 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(0,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 →