Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active June 9, 2016 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/3aa6f253b0beb0af5430 to your computer and use it in GitHub Desktop.
Save thmain/3aa6f253b0beb0af5430 to your computer and use it in GitHub Desktop.
public class PrintAllPathIn2DArray {
int rowCount;
int colCount;
int[][] arrA;
public PrintAllPathIn2DArray(int arrA[][]) {
this.arrA = arrA;
rowCount = arrA.length;
colCount = arrA[0].length;
}
public void printAll(int currentRow, int currentColumn, String path) {
if (currentRow == rowCount - 1) {
for (int i = currentColumn; i < colCount; i++) {
path += "-" + arrA[currentRow][i];
}
System.out.println(path);
return;
}
if (currentColumn == colCount - 1) {
for (int i = currentRow; i <= rowCount - 1; i++) {
path += "-" + arrA[i][currentColumn];
}
System.out.println(path);
return;
}
path = path + "-" + arrA[currentRow][currentColumn];
printAll(currentRow + 1, currentColumn, path);
printAll(currentRow, currentColumn + 1, path);
// printAll(currentRow + 1, currentColumn + 1, path);
}
public static void main(String args[]) {
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 } };
PrintAllPathIn2DArray p = new PrintAllPathIn2DArray(a);
p.printAll(0, 0, "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment