Print all possible paths from top left to bottom right of a m x n char[][]
board
import java.util.ArrayList;
/**
* Created by yang on 1/2/17.
*/
public class Solution {
public static ArrayList<String> printCharBoard(char[][] board) {
ArrayList<String> rst = new ArrayList<>();
if (board == null || board.length == 0) {
return rst;
}
StringBuilder sb = new StringBuilder();
dfs(board, 0, 0, sb, rst);
return rst;
}
private static void dfs(char[][] board, int row, int col, StringBuilder sb, ArrayList<String> rst) {
if (row == board.length - 1) {
for (int i = col; i < board[0].length; i++) {
sb.append(board[row][i]);
}
rst.add(sb.toString());
return;
}
if (col == board[0].length - 1) {
for (int i = row; i < board.length; i++) {
sb.append(board[i][col]);
}
rst.add(sb.toString());
return;
}
sb.append(board[row][col]);
int len = sb.length();
dfs(board, row + 1, col, sb, rst);
sb.setLength(len); // 非常重要,用来保留上一次的分叉口
dfs(board, row, col + 1, sb, rst);
sb.setLength(len); // 非常重要,用来保留上一次的分叉口
}
public static void main(String[] args) {
char[] arr1 = new char[]{'a', 'b', 'c'};
char[] arr2 = new char[]{'d', 'e', 'f'};
char[] arr3 = new char[]{'g', 'h', 'i'};
char[][] board = new char[][]{arr1, arr2, arr3};
ArrayList<String> rst = printCharBoard(board);
for (String s : rst) {
System.out.println(s);
}
}
}