public class Manhattan { public static void main (String[] argv) { // Test case 1: go from (1,1) to (0,0) int r = 1, c = 1; int n = countPaths (r, c); System.out.println ("r=" + r + " c=" + c + " => n=" + n); // Test case 2: go from (2,2) to (0,0) r = 2; c = 2; n = countPaths (r, c); System.out.println ("r=" + r + " c=" + c + " => n=" + n); // Test case 2: go from (5,7) to (0,0) r = 5; c = 7; n = countPaths (r, c); System.out.println ("r=" + r + " c=" + c + " => n=" + n); } static int countPaths (int numRows, int numCols) { // Bottom out case: there's only one way to (0,0). // Note: it's || and not &&. if ( (numRows == 0) || (numCols == 0) ) { return 1; } // Otherwise, reduce to two sub-problems and add. int downCount = countPaths (numRows-1, numCols); int rightCount = countPaths (numRows, numCols-1); return (downCount + rightCount); } }