Java 1D Array (Part 2)

Sort by

recency

|

627 Discussions

|

  • + 0 comments

    public static boolean canWin(int leap, int[] game) { return canWinHelper(leap, game, 0); }

    // Helper method to recursively check if the game can be won private static boolean canWinHelper(int leap, int[] game, int pos) { // Base cases if (pos < 0 || game[pos] == 1) return false; // Out of bounds or already visited if (pos >= game.length - 1 || pos + leap >= game.length) return true; // Win condition

    // Mark the current position as visited
    game[pos] = 1;
    
    // Recursive checks for possible moves
    return canWinHelper(leap, game, pos + leap) || // Jump forward
           canWinHelper(leap, game, pos + 1) ||   // Walk forward
           canWinHelper(leap, game, pos - 1);     // Walk backward
    

    }

  • + 0 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.
    }
    
    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();
    }
    

    }

  • + 1 comment

    reposting a solution from @kdubey2181, that I think is the most elegant and simple way to solve this problem.

    import java.util.*;
    
    public class Solution {
    
        public static boolean canWin(int leap, int[] game, int position) {
    
            int lastIndex = game.length-1;
            
            //did you win by jumping over the last index?
            if(position>lastIndex){
                return true;
            }
            
            //did you reverse out of bounds or meet a obstacle?
            if(position<0 || game[position]==1){
                return false;
            }
            
            //mark current position as explored
            game[position] = 1;
            
            //Explore all possible solutions from current position
            return canWin(leap, game, position+1) || canWin(leap, game, position-1) || canWin(leap, game, position+leap);
        }
    
        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, 0)) ? "YES" : "NO" );
            }
            scan.close();
        }
    }
    
  • + 1 comment

    whats wrong with my iterative approach can anyone point out?

            int n = game.length;
            int pointer = 0;
            while(pointer<=n){
                if(pointer+leap>=n || pointer+1>=n) return true;
                if(pointer+leap<n && game[pointer+leap]==0){
                    pointer+=leap;
                }
                else if(pointer+1<n && game[pointer+1]==0){
                    pointer+=1;
                }
                else if(pointer-1>=0 && game[pointer-1]==0){
                    pointer-=1;
                }
                else{
                    return false;
                }
                game[pointer]=1;
            }
            return true;
    
  • + 0 comments
    import java.util.*;
    
    public class Solution {
    
        public static boolean find_path(int leap, int[] game, int x) {
            if (x < 0) {
                return false;
            }
    
            if (x > game.length - 1) {
                return true;
            }
    
            if (game[x] != 0) {
                return false;
            }
    
            game[x] = 5;
    
            if (find_path(leap, game, x + 1)) {
                return true;
            }
            if (find_path(leap, game, x + leap)) {
                return true;
            }
            if (find_path(leap, game, x - 1)) {
                return true;
            }
    
            game[x] = 0;
    
            return false;
        }
    
        public static boolean canWin(int leap, int[] game) {
            return find_path(leap, game, 0);
        }
    
    
        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();
        }
    }