You are viewing a single comment's thread. Return to all comments →
hi everyone..... hope my code helps you all better.....
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] game, int pos, boolean[] vis) { vis[pos]=true; try{ if(game[pos+leap]==0&&vis[pos+leap]==false){ if(canWin(leap, game, pos+leap, vis)){ return true; } else{ return canWin(leap, game, pos, vis); } } else if(game[pos+1]==0&&vis[pos+1]==false){ if(canWin(leap, game, pos+1, vis)){ return true; } else{ return canWin(leap, game, pos, vis); } } else if(pos!=0&&vis[pos-1]==false&&game[pos-1]==0){ if(canWin(leap, game, pos-1, vis)){ return true; } else{ return canWin(leap, game, pos, vis); } } return false; } catch(ArrayIndexOutOfBoundsException e){ if(pos+leap>game.length-1){ return true; } else if(pos+1>game.length-1){ return true; } else{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]; boolean[] vis=new boolean[n]; for (int i = 0; i < n; i++) { game[i] = scan.nextInt(); vis[i]=false; } vis[0]=true; System.out.println( (canWin(leap, game,0,vis)) ? "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 →
hi everyone..... hope my code helps you all better.....
import java.util.*;
public class Solution {
}