Question

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

My paths from A to F would be: A - > B -> C -> F or A -> D -> E -> F or A ->B -> E - > F so on...

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

Thank you,

Homework Answers

Answer #1

Answer:

import java.util.Stack;

class Main
{
        public static void findPaths(char[][] mat, Stack<Character> path,
                                                                 int i, int j)
        {
                int M = mat.length;
                int N = mat[0].length;

                // if we have reached the last cell, print the route
                if (i == M - 1 && j == N - 1)
                {
                        path.add(mat[i][j]);
                        System.out.println(path);
                        path.pop();

                        return;
                }

                // include current cell in path
                path.add(mat[i][j]);

                // move right
                if ((i >= 0 && i < M && j + 1 >= 0 && j + 1 < N)) {
                        findPaths(mat, path, i, j + 1);
                }

                // move down
                if ((i + 1 >= 0 && i + 1 < M && j >= 0 && j < N)) {
                        findPaths(mat, path, i + 1, j);
                }

                // remove current cell from path
                path.pop();
        }

        public static void main(String[] args)
        {
                char[][] mat =
                {
                        { 'A','B','C' },
                        { 'D','E','F' },
                };

                Stack<Character> path = new Stack<>();

                // start from (0, 0) cell
                int x = 0, y = 0;

                findPaths(mat, path, x, y);
        }
}

Output:

Please give thumbsup, or do comment in case of any query. Thanks.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a) How many grid paths from (0,0) to (6,6) are there? b) c) How many go...
a) How many grid paths from (0,0) to (6,6) are there? b) c) How many go through one of the points (1,1) or (2,2) or (3,3)? I need answer for C) please. I'm trying to use the A + B + C - AnB - AnC - BnC + AnBnC rule (Principle of Inclusion/Exclusion) for this problem but I found out that AnC(from 1,1 to 3,3 ) is just equal to AnBnC(from 1,1 to 2,2 to 3,3) because for grid...
Consider a grid with vertices from (0,0) to (20,15), in which edges can only be traversed...
Consider a grid with vertices from (0,0) to (20,15), in which edges can only be traversed up or right. Think of a street grid with intersections labelled by pairs of integers, and all one-way streets. At grid point (5,7) there is a forbidden node (e.g. a blocked intersection). How many paths, going only up and right, are there in the grid which do not pass through the forbidden intersection?
STARTING AT A, IT IS POSSIBLE TO GO THROUGH ALL THE BOARDS MOVING FROM CELL VERTICALLY OR HORIZONTALLY.
ABCDEEFGHIJKLMNOPQRSSTARTING AT A, IT IS POSSIBLE TO GO THROUGH ALL THE BOARDS MOVING FROM CELL VERTICALLY OR HORIZONTALLY.VERIFI THIS!IS IT TRUE THAT A PATH LIKE THIS CAN START AT ANY CELL?
In this assignment, you will write pseudo-code for Markov Decision Process. A Markov Decision Process also...
In this assignment, you will write pseudo-code for Markov Decision Process. A Markov Decision Process also known as MDP model contains the following set of features: A set of possible states S. A set of Models. A set of possible actions A. A real valued reward function R (s, a). A solution of Markov Decision Process. Consider the following Grid (3 by 3): Fire Diamond 3    2 Start Blocked 1 1 2 3 An agent lives in a grid....
Section B. For each question in this section, you are required to list all possible candidate...
Section B. For each question in this section, you are required to list all possible candidate keys for the given schema based on the functional dependencies provided. You may wish to compute the closure of your key(s) to confirms they are valid. Question 1 R [A, B, C, D, E, F, G, H, I, J] {A} -> {B} {C} -> {B} {B} -> {D, E, F, G, H} {D, F} -> {I, J, A} Question 2 R [A, B, C,...
Python: Working with CSV file --> How would I write a function that will return a...
Python: Working with CSV file --> How would I write a function that will return a list of lists, with the frequency and the zip code. Organize the list in decreasing order of frequency (Print the number of suppliers and the zip code for the 10 most common zip codes in the file. ) An example of three items from the 720 zip codes results in: [ ... [9, '65616'], [8, '94573'], [8, '63103'] ...]. This tells us that Branson,...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
My question that needs an answer is this: Can the C++ compilers catch the mistake of...
My question that needs an answer is this: Can the C++ compilers catch the mistake of using an "=" instead of the comparison operator "==". Show code how you can test this using an if statement in C++, write a small program to demonstrate it. If the compiler does not catch the mistake how would one go about ensuring they are using the correct syntax for the comparison operator? Thank you
Consider a consumer whose preference for burgers from “In-N-Out” and “The Habit” are such that she...
Consider a consumer whose preference for burgers from “In-N-Out” and “The Habit” are such that she can perfectly substitute out one for another. That being said, the utility that she receives from consuming an In-N-Out burger (I) is four times that of the utility she receives from consuming a burger from The Habit (H). (a) Write down the utility function. (b) Find the MRS. (c) Suppose I = $60,PI = $6,&PH = $5. Write down the budget con- straint. (d)...
How can I use this topic in a study or experiment scenario: there is a linkage...
How can I use this topic in a study or experiment scenario: there is a linkage between child abuse and adolescent dating violence? I did some research, the articles suggest abuse occurring during childhood can lead to adolescent dating violence in middle school to high school. Creating either the perpetrator in the relationship, or the victim. I think I should narrow my study to child abuse leading to the perpetrator in the adolescent relationship. Although, how could I set this...