Matrix print all possible paths from top left to bottom right - java

public class PrintAllPathMatrix {

static void printPaths (String tempString, int i, int j, int m, int n, char [][] arr) {
String newString = tempString + arr[i][j]; if (i == m -1 && j == n-1) {
System.out.println(newString); return; }
//right if (j+1 < n) {
printPaths (newString, i, j+1, m, n, arr); }
//down if (i+1 < m) {
printPaths (newString, i+1, j, m, n, arr); }
}
public static void main (String[] args) throws java.lang.Exception
{
char [] [] pathInput = {
{'a', 'b', 'c', 'z'}, {'d', 'e', 'f', 'y'}, {'g', 'h', 'i', 'x'}, };
printPaths ("", 0, 0, pathInput.length, pathInput[0].length, pathInput); }
}

Comments

Popular posts from this blog

EJB - Stateful vs Stateless

Inversion of Control vs Dependency Injection