Untitled

 avatar
unknown
plain_text
2 years ago
3.3 kB
6
Indexable
import java.util.*;

public class RobotInAGrid {
    static boolean ansf = false;
    // Implement your Solution Here
    Vector<String> robotInAGrid(int grid[][] , int n , int m) {
        ansf = false;
        Vector<String> ans = new Vector<>();
        helper(grid,0,0,ans);
        if(ansf == false || grid[n-1][m-1] == 1 ||grid[0][0] ==1 ) {
            ans.clear();
            ans.add("Not Possible");
            return ans;
        }
        return ans;
    }
    public static void helper(int grid[][],int i,int j,Vector<String> ans){
        if (i == grid.length - 1 && j == grid[0].length - 1) {
            ans.add(new String((i+1) + " " + (j+1)));
            ansf=true;
            return;
        }

        if (grid[i][j] == 1) {
            return;
        }
        if (ansf == false && i < grid.length - 1) {
            ans.add(new String((i+1) + " " + (j+1)));
            helper(grid, i+1, j,ans);
            if(ansf == false)
            ans.remove(ans.size()-1);
        }

        if (ansf == false && j < grid[0].length - 1) {
            ans.add(new String((i+1) + " " + (j+1)));
            helper(grid, i, j+1,ans);
            if(ansf == false)
            ans.remove(ans.size()-1);
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int grid[][] = new int[n][m];
        for(int i = 0 ; i < n ; i++) {
            for(int j = 0 ; j < m ; j++) {
                grid[i][j] = scanner.nextInt();
            }
        }
        scanner.close();
        RobotInAGrid answer = new RobotInAGrid();
        Vector<String> result = answer.robotInAGrid(grid , n , m);
        for(String elem : result)
        {
            System.out.println(elem);
        }
    }
}

/* 
  Crio Methodology
  
  Milestone 1: Understand the problem clearly
  1. Ask questions & clarify the problem statement clearly.
  2. *Type down* an example or two to confirm your understanding of the input/output & extend it to test cases
  
  Milestone 2: Finalize approach & execution plan
  1. Understand what type of problem you are solving.
       a. Obvious logic, tests ability to convert logic to code
       b. Figuring out logic
       c. Knowledge of specific domain or concepts
       d. Knowledge of specific algorithm
       e. Mapping real world into abstract concepts/data structures
  2. Brainstorm multiple ways to solve the problem and pick one
  3. Get to a point where you can explain your approach to a 10 year old
  4. Take a stab at the high-level logic & *type it down*.
  5. Try to offload processing to functions & keeping your main code small.
  
  Milestone 3: Code by expanding your pseudocode
  1. Have frequent runs of your code, dont wait for the end
  2. Make sure you name the variables, functions clearly.
  3. Avoid constants in your code unless necessary; go for generic functions, you can use examples for your thinking though.
  4. Use libraries as much as possible
  
  Milestone 4: Prove to the interviewer that your code works with unit tests
  1. Make sure you check boundary conditions
  2. Time & storage complexity
  3. Suggest optimizations if applicable
  */
Editor is loading...