Last active
June 9, 2016 02:24
-
-
Save thmain/3aa6f253b0beb0af5430 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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