Challenge :
Given a 2D array m containing non-negative integers , we define a βpathβ as a set of neighboring cells ( do diagonal steps not be considered a neighbor ) starting from row == 0 && col == 0 and ending with row == m.length - 1 && col == m[0].length - 1 .
the cost of a "path" is the sum of the values ββin each cell of the "path".
Example:
Two possible paths in an array: 
The cost of path 1 (dashed line): 8 + 4 + 2 + 8 + 9 + 9 + 7 + 5 = 52;
Cost of path 2 (full line): 8 + 6 + 3 + 8 + 9 + 4 + 1 + 2 + 1 + 7 + 6 + 5 = 60
MAKE:
Write a recursive static method that takes a 2D array m filled with non-negative integer values and prints the sum of all possible costs on the path (you can assume that m not null and not empty).
Method signature (overload allowed):
public static void printPathWeights(int [][] m)
My code is:
public class Main { public static void main(String[] args) { int arr[][] = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } }; printPathWeights(arr); } public static void printPathWeights(int[][] m) { System.out.println(printPathWeights(m, 0, 0, new int[m.length][m[0].length], 0)); } /* * @param map marks the visited cells */ private static int printPathWeights(int[][] m, int row, int col, int[][] map, int carrier) { if (row < 0 || col < 0 || row >= m.length || col >= m[0].length || map[row][col] == 1) return 0; if (row == m.length - 1 && col == m[0].length - 1) return m[row][col] + carrier; map[row][col] = 1; return printPathWeights(m, row + 1, col, map, carrier + m[row][col]) + printPathWeights(m, row - 1, col, map, carrier + m[row][col]) + printPathWeights(m, row, col + 1, map, carrier + m[row][col]) + printPathWeights(m, row, col - 1, map, carrier + m[row][col]); } }
Print value of above code: 14
This is less than expected!
When starting with:
int arr[][] = { { 1, 1 }, { 1, 1 } };
The result is 6.
What is wrong in my code?
PS: please do not tell me the code that decides the purpose, but explain what is wrong with my code.