Question

In: Computer Science

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 number of paths.Constraints:
1 ≤ T ≤ 30
1 ≤ M,N ≤ 10Example:
Input
2
3 3
2 8Output
6
8Explanation:
Testcase 1: Let the given input 3*3 matrix is filled as such:
A B C
D E F
G H I
The possible paths which exists to reach 'I' from 'A' following above conditions are as follows:
ABCFI, ABEHI, ADGHI, ADEFI, ADEHI, ABEFI.

Solutions

Expert Solution

// Defines a class AllPossiblePath to display all possible path

// in a matrix

public class AllPossiblePath

{

/*

* Recursive method to print all possible path

* matrix: Contains data for the matrix of size row x col

* row and col: Represents row and column dimension of the matrix

* r and c: Represents the current index position of row and column

* currentPath: To store the current path

*/

private static void printAllMatrixPath(int matrix[][], int row, int col,

int r, int c, int currentPath[], int index)

{

// Stores the matrix current r and c index position value

// at index position of currentPath array

currentPath[index] = matrix[r][c];

  

// In order to move right checks if current row index is less then the

// maximum number of rows minus one

if (r == row - 1)  

{

// Reached bottom of the matrix so, left with

// only option to move right

for (int k = c + 1; k < col; k++)

currentPath[index + k - c] = matrix[r][k];

  

for (int l = 0; l < index + col - c; l++)

System.out.print(currentPath[l] + " ");

System.out.println();

return;

}// End of if condition

  

  

// In order to move downward checks if current column index is less then the

// maximum number of columns minus one

if (c == col - 1)  

{

// Reached the right corner of the matrix so, left with

// only the downward movement.

for (int k = r + 1; k < row; k++)  

currentPath[index + k - r] = matrix[k][c];

  

for (int l = 0; l < index + row - r; l++)

System.out.print(currentPath[l] + " ");

System.out.println();

return;

}// End of if condition  

  

// Recursively call the method to display all the paths

// possible after moving down

printAllMatrixPath(matrix, row, col, r + 1, c, currentPath, index + 1);

  

// Recursively call the method to display all the paths

// possible after moving right

printAllMatrixPath(matrix, row, col, r, c + 1, currentPath, index + 1);

}// End of method

  

// main method definition

public static void main(String[] args)  

{

// Number of rows and columns

int row = 3;

int col = 3;

// Creates a matrix

int matrix[][] = { { 1, 2, 3 },  

{ 4, 5, 6 },

{ 7, 8, 9 }

};

// Calculates the maximum path length

int maxPathLength = row + col - 1;

// Calls the method to print all possible path in matrix

printAllMatrixPath(matrix, row, col, 0, 0, new int[maxPathLength], 0);

}// End of main method

}// End of class

Sample Output:
1 4 7 8 9
1 4 5 8 9
1 4 5 6 9
1 2 5 8 9
1 2 5 6 9
1 2 3 6 9


Related Solutions

Java programming question:Consider the inheritance hierarchy in the figure on the next page.The top...
Java programming question:Consider the inheritance hierarchy in the figure on the next page. The top of the hierarchy begins with class Shape, which is extended by subclasses TwoDShape and ThreeDShape, corresponding to 2D and 3D shapes, respectively. The third level of this hierarchy contains specific types of 2D and 3D shapes such as circles and spheres, respectively. The arrows in the graph represent is a(inheritance) relationships and the boxes represent classes. For example, a Triangle is a TwoDShape, which in...
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
For light moving from left to right through a lens, describe the paths of the three...
For light moving from left to right through a lens, describe the paths of the three principal rays used to locate the image formed by the lens. Using the three principal rays, draw two ray diagrams for object sitting in front of (a) concave lens (b)convex lens. Please indicate the focus points clearly on your ray diagrams. Describe the difference between the shapes of converging and diverging lens , as well as the difference between the images formed by converging...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am getting a illegal expression error and don't know how to fix it, also may be a few other error please help CODE BELOW import java.util.Scanner; public class Event { public static double pricePerGuestHigh = 35.00; public static double pricePerGuestLow = 32.00; public static final int LARGE_EVENT_MAX = 50; public String phnum=""; public String eventNumber=""; private int guests; private double pricePerEvent; public void setPhoneNumber() {...
This java program try to count all of the 1 at the rim of the block...
This java program try to count all of the 1 at the rim of the block of 1 surrounded by 0. The total of the count also includes all the 1 surrounded two pockets of 0 within the block. The correct answer fro the total count is 154. However, this program gives 167. Where is the fault of the program?    public class Cloture {     public static void main(String[] args) { int[][] carte = {                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0},                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},...
CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years,...
CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years, 10 per line, from 1001 to 2100, separated by exactly one space. Also display the total number of leap years in this period. Hints: you need to use a loop ( for-loop is more suitable)
PHP Programming question! Using Loop count from 0 to 300 by 15's ex) 123456789101112131415 161718192021222324252627282930 .....
PHP Programming question! Using Loop count from 0 to 300 by 15's ex) 123456789101112131415 161718192021222324252627282930 .....
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
In java please Question: You are given a string s. Your task is to count the...
In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT