Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q9...IN JAVA Answer the following: using the code below public class Manhattan2 { public static void main (String[] argv) { // Test case 1: int
Q9...IN JAVA
Answer the following: using the code below
public class Manhattan2 { public static void main (String[] argv) { // Test case 1: int r = 1, c = 1; int n = countPaths (r, c, "[1,1]"); System.out.println ("r=" + r + " c=" + c + " => n=" + n); // Test case 2: r = 2; c = 2; n = countPaths (r, c, "[2,2]"); System.out.println ("r=" + r + " c=" + c + " => n=" + n); } static int countPaths (int numRows, int numCols, String partialPath) { // Bottom out case: this is more complicated now. if (numRows == 0) { // Make the path across the columns. String finalStr = partialPath; for (int c=numCols-1; c>=0; c--) { finalStr += " -> [0," + c + "]"; } System.out.println (finalStr); return 1; } else if (numCols == 0) { // Make the path down rows. String finalStr = partialPath; for (int r=numRows-1; r>=0; r--) { finalStr += " -> [" + r + ",0]"; } System.out.println (finalStr); return 1; } // Otherwise, reduce problem size. // Downwards. String downpathStr = partialPath + " -> " + "[" + (numRows-1) + "," + numCols + "]"; int downCount = countPaths (numRows-1, numCols, downpathStr); // Rightwards. String rightpathStr = partialPath + " -> " + "[" + (numRows) + "," + (numCols-1) + "]"; int rightCount = countPaths (numRows, numCols-1, rightpathStr); // Add the two. return (downCount + rightCount); } }In-Class Exercise 9: Suppose we want the output to number the paths as follows: Path Path Path Path Path Path #1: #2 : #3: #4: #5: #6: [2,2] [2,2] [2,2] [2,2] [2,2] [2,2] -> -> -> -> -> -> [1,2] [1,2] [1,2] [2,1] [2,1] [2,1] -> -> -> -> -> -> [0,2] [1,1] [1,1] [1,1] [1,1] [2,0] -> -> -> -> -> -> [0,1] [0,1] [1,0] [0,1] [1,0] [1,0] -> -> -> -> -> -> [0,0] [0,0] [0,0] [0,0] [0,0] [0,0] Modify Manhattan2.java above to number paths
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started