Question

In: Computer Science

How can I return in a list all possible paths from a grid if I can...

How can I return in a list all possible paths from a grid if I can only go right and down?

For example consider the following table:

A B C

D E F

If I go right I need to insert in my list 'H' if I go down I need to insert in my list 'V'.

For example the path A - > B -> C -> F would be H - H - V

The path A -> D -> E -> F would be V - H - H

How would be the code for that? Please preference write in JAVA.

Thank you,

Solutions

Expert Solution

Complete code in JAVA:-

import java.util.*;

// Main class
class Path {

   // 'move' method takes next possible move in either of the direction from 'i', 'j'
   // move horizontally or vertically.
   public static void move(String path, char [][]mat, int i, int j, int n, int m, ArrayList<String> lists) {

       // Adding current character into path
       path = (path+mat[i][j]+"->");

       // This variable will keep track if there is further a move possible or not.
       boolean canMove = false;

       // If possible move vertically.
       if(i < n-1) {
           move(path, mat, i+1, j, n, m, lists);
           canMove = true;
       }

       // If possible move horizontally.
       if(j < m-1) {
           move(path, mat, i, j+1, n, m, lists);
           canMove = true;
       }

       // If no further move was possible
       // 'path' is one possible path, so add it to 'lists'.
       if(!canMove) {
           lists.add(path.substring(0, path.length()-2));
       }
   }

   // Main function.
   public static void main(String ... args) {

       // This is the matrix in which we have to find all possible paths.
       char [][]mat = new char[2][3];
       mat[0][0] = 'A';
       mat[0][1] = 'B';
       mat[0][2] = 'C';
       mat[1][0] = 'D';
       mat[1][1] = 'E';
       mat[1][2] = 'F';

       // 'paths' list will store all possible paths.
       ArrayList<String> paths = new ArrayList<String> ();

       // Exploring all possible paths.
       String path = "";
       move(path, mat, 0, 0, 2, 3, paths);

       // Print all the possible paths.
       System.out.println("All possible paths : ");
       for(int i = 0; i < paths.size(); i++) {
           System.out.println(paths.get(i));
       }
   }
}

Screenshot of output:-


Related Solutions

(a) Given a 4×4 grid, how many different paths from (0,0) to (4,4) satisfy the following...
(a) Given a 4×4 grid, how many different paths from (0,0) to (4,4) satisfy the following condition: • You can only go from (x, y) to either (x+1, y) or (x, y+1) (b) Given a 4×4 grid, how many different paths from (0,0) to (4,4) satisfy the following condition: • You can only go from (x, y) to either (x+1, y) or (x, y+1) • You cannot go to points (x, y) where y > x, in other word, you...
Java Programming Question The problem is to count all the possible paths from top left to...
Java Programming Question The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down.Input: The first line of input contains an integer T, denoting the number of test cases. The first line of each test case is M and N, M is number of rows and N is number of columns.Output: For each test case, print the...
How do you add all the values that return by all the threads from threadpools? I...
How do you add all the values that return by all the threads from threadpools? I have created a threadpool with 1000 of threads and run the task 1000 times. However, when I try to print out the result it gives a whole bunch of values that return by all the threads, it looks like... Thread-1 xxxx Thread-2 xxxx Thread-3 xxxx Thread-4 xxxx . . . . . Is there a possible way to get a single value back instead...
Among all the possible permutations that can be made by all the characters from the series...
Among all the possible permutations that can be made by all the characters from the series of words "COMPUTER", how many different ways that always and only three characters are in between 'P' and 'R''? Show both your work and the exact number.
how can i calculate a "required levered return"?
how can i calculate a "required levered return"?
What are some possible career paths one can take with an educational background in finance? What...
What are some possible career paths one can take with an educational background in finance? What are some of the key steps someone should take in order to prepare for a career in finance? What is the primary role a publicly traded company should pursue, and why?
Under conditions of risk, the manager can make a list of all possible outcomes and assign...
Under conditions of risk, the manager can make a list of all possible outcomes and assign probabilities to the various outcomes. How do we measure risk?
Describe how the health disruption might be prevented and list all possible interventions.
Describe how the health disruption might be prevented and list all possible interventions.
List all the possible formulas that could be used for the following scenario. Chapters from chapters...
List all the possible formulas that could be used for the following scenario. Chapters from chapters Motion in a cirlce, torque, gravity. "In the winter Olympics, skaters go around the curved part of a flat oval track at top speed without sliding."
How is a monopoly different from perfect competition? List all the ways you can think of....
How is a monopoly different from perfect competition? List all the ways you can think of. Why do we study them together? What do they tell us about competition?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT