To count all the possible paths from top left to bottom right of a m x n matrix with the constraints that from each cell we can either move only to right or down and cannot go back.
N and M, denoting the number of rows and number of column respectively. count of all the possible paths from top left to bottom right of a m x n matrix.
Input : m = 2, n = 2;
Output : 2
There are two paths
(0, 0) --> (0, 1) --> (1, 1)
(0, 0) --> (1, 0) --> (1, 1)
Once approach is to generate all paths and then, determine which paths are valid.
The keys involved are:
Java program to Print all possible paths from
top left to bottom right of a mXn matrix
Java program to Print all possible Routes from
top left to bottom right of a mXn matrix
starting of mXn matrix
i, j: Current position (For the first call use 0,0)
m, n: Dimentions of given the matrix
Here m = 6 and n = 4, we start from (0, 0) and go to the end (5, 3) we can consider any one path lets say we choose
(0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2) -> (3, 2) -> (4, 2)
Therefore, we moved 3 steps to the right and 5 steps downwards. Even if we take any other path same number of right and down steps will be required.
======================================================
public class FindPath
{ private static void showRoute(int mat[][], int m, int n, int i, int j, int Route[], int idx) {
Route[idx] = mat[i][j];
if (i == m - 1) {
for (int k = j + 1; k < n; k++) {
Route[idx + k - j] = mat[i][k];
}
for (int l = 0; l < idx + n - j; l++) {
System.out.print(Route[l] + " ");
}
System.out.println();
return;
// Reached the right corner of the matrix we are left with only the downward movement.
if (j == n - 1) {
for (int k = i + 1; k < m; k++) {
Route[idx + k - i] = mat[k][j];
}
for (int l = 0; l < idx + m - i; l++) {
System.out.print(Route[l] + " ");
}
System.out.println();
return;
}
// Print all the Routes that are possible after moving down
showRoute(mat, m, n, i + 1, j, Route, idx + 1);
// Print all the Routes that are possible after moving right
showRoute(mat, m, n, i, j + 1, Route, idx + 1);
}
// Test code
public static void main(String[] args) {
int m = 4; //number of rows
int n = 6; //number of cols
int mat[][] = { { 1, 2, 3, 4,5,6 }, { 7, 8, 9 ,10,11,12}, {13,14,15,16,17,18},{19,20,21,22,23,24}}; //m * n matrix
int totalLengthOfRoute = m + n - 1;
showRoute(mat, m, n, 0, 0, new int[totalLengthOfRoute], 0);
}
}
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
1 7 13 19 20 21 22 23 24 |
1 7 13 14 20 21 22 23 24 |
1 7 13 14 15 21 22 23 24 |
1 7 13 14 15 16 22 23 24 |
1 7 13 14 15 16 17 23 24 |
1 7 13 14 15 16 17 18 24 |
1 7 8 14 20 21 22 23 24 |
1 7 8 14 15 21 22 23 24 |
1 7 8 14 15 16 22 23 24 |
1 7 8 14 15 16 17 23 24 |
1 7 8 14 15 16 17 18 24 |
1 7 8 9 15 21 22 23 24 |
1 7 8 9 15 16 22 23 24 |
1 7 8 9 15 16 17 23 24 |
1 7 8 9 15 16 17 18 24 |
1 7 8 9 10 16 22 23 24 |
1 7 8 9 10 16 17 23 24 |
1 7 8 9 10 16 17 18 24 |
1 7 8 9 10 11 17 23 24 |
1 7 8 9 10 11 17 18 24 |
1 7 8 9 10 11 12 18 24 |
1 2 8 14 20 21 22 23 24 |
1 2 8 14 15 21 22 23 24 |
1 2 8 14 15 16 22 23 24 |
1 2 8 14 15 16 17 23 24 |
1 2 8 14 15 16 17 18 24 |
1 2 8 9 15 21 22 23 24 |
1 2 8 9 15 16 22 23 24 |
1 2 8 9 15 16 17 23 24 |
1 2 8 9 15 16 17 18 24 |
1 2 8 9 10 16 22 23 24 |
1 2 8 9 10 16 17 23 24 |
1 2 8 9 10 16 17 18 24 |
1 2 8 9 10 11 17 23 24 |
1 2 8 9 10 11 17 18 24 |
1 2 8 9 10 11 12 18 24 |
1 2 3 9 15 21 22 23 24 |
1 2 3 9 15 16 22 23 24 |
1 2 3 9 15 16 17 23 24 |
1 2 3 9 15 16 17 18 24 |
1 2 3 9 10 16 22 23 24 |
1 2 3 9 10 16 17 23 24 |
1 2 3 9 10 16 17 18 24 |
1 2 3 9 10 11 17 23 24 |
1 2 3 9 10 11 17 18 24 |
1 2 3 9 10 11 12 18 24 |
1 2 3 4 10 16 22 23 24 |
1 2 3 4 10 16 17 23 24 |
1 2 3 4 10 16 17 18 24 |
1 2 3 4 10 11 17 23 24 |
1 2 3 4 10 11 17 18 24 |
1 2 3 4 10 11 12 18 24 |
1 2 3 4 5 11 17 23 24 |
1 2 3 4 5 11 17 18 24 |
1 2 3 4 5 11 12 18 24 |
1 2 3 4 5 6 12 18 24 |
56
Need help with this for practice! Please answer swiftly or I will not upvote. A computer...
A computer programmer lives in his home located at the upper left corner of the following street grid and works in a building located at the right lower corner. In the grid, lines represent streets. There are five horizontal streets and seven vertical streets. On his way to work, he can drive along any street and turn at any intersection, but can only go down or right, and cannot go back. How many different paths are there for him to...
please i need help in all three this is the second time i post this so please help and do it right this time and show all the work Sketch the graph of the polar curve r=3 for OSOS 4x and indicate the orientation. Set up an integral to determine how long the curve is, is unrolled (10 pts.] 6. Graph the polar equations r-Sco) and = 3 col) for #/2515./2. Label any key points needed to find the area...
PLEASE HELP IN Answer the following questions based on this case study WHAT SHOULD BE MOST IMPORTANT TO CONSIDER FOR THIS PATIENT. RJ is a 79 year old man who fell when he got out of bed to go to the bathroom at home. He arrived to the unit with Buck’s traction to his R lower leg, and now has had surgery for a right hip fracture two days ago. He lives alone since his wife died, and his children...
Pleade,can you help me to answer these questions Please, Please , I need help 11:374 ..1 LTED mail-attachment.googleusercontent.com When the them during the time of the paper e The of his loyal flowe A. The Arabe The Mega Whew is Princesa Te C ry To lead the Cle narpathare the நாளெ m e ன ப thaay AC Richading price is from of the Disewwing was wych So why Prish How did a lot of the Sampollet As the conditions and...
I need help on part B (C-F) please. I will rate the question if I get it wrong or right on my homework. MINICASE Scandi Home Furnishings, Inc. high grade plastics Kaj Rasmussen founded Scandi Home Furnishings as a corporation during mid 2013. Sales during the first full year (2018) of operation reached $1.3 million Sales increased by 15 percent in 2015 and another 20 percent in 2016. However, after increasing in 2015 over 2014, profits fall sharply in 2016,...
please i need a good answer and a perfect answer.. i need unique answer use your own words. don't use handwriting Case Study Imagine working in an organization where employee morale is low, turnover is high, and the costs of hiring are astronomical. If that were the case, you’d imagine the employer would go to great lengths to find, attract, and retain quality employees. Couple this goal with the reality of the economic picture—you simply cannot afford to provide expensive...
Please answer the following question below based on the information and figure. I just need a description of which bulb will go brighter in each of the pictures. And any equations that would be necessary to explain this. It does not need to be worked out with numbers. Just the equations and words explaining how you can tell which glows brighter. Thanks! Below is also an example of how to set it up! UQAWA - Electricity Spring 2019 Pictured is...
Need some help with this assignment please. I did the other 2 sections fine but this one i am having some issues with. We have a scenario where a client has emailed us. In the email response back to the client we have to do the following. 1. ID what additional documents the client needs to provide in order to do their taxes 2. ID applicable deductions and credits available for clients 3. differentiate typed of income and expenditures. MY...
I need help with Question 7, please Harold is a 67-year-old male who has had regular physical exams, is a non-smoker, and who has been in good health for most of his life. In recent years, he has been experiencing symptoms of heartburn, nausea, and indigestion after eating certain foods. Although he experienced some relief after changing his diet to avoid those foods, the symptoms did not completely subside. Harold was eventually diagnosed with having a form of chronic atrophic...
Hello dear, Please i need help to solve this problem in Finance 1. Apply What You’ve Learned - Auto Purchase Scenario: You are in the market for a new car. You do not have a trade-in, but you have saved $2,500 toward a down payment. You currently earn $4,000.00 gross monthly income, of which 35% is withheld for various deductions. You have heard of the 20% rule of thumb, but want to limit your payments to no more than 15%...