本文參考自《劍指offer》一書,代碼采用Java語言。
題目
請設計一個函數,用來判斷在一個矩陣中是否存在一條包含某字符串所有字符的路徑。路徑可以從矩陣中任意一格開始,每一步可以在矩陣中向左、右、上、下移動一格。如果一條路徑經過了矩陣的某一格,那么該路徑不能再次進入該格子。例如在下面的3×4的矩陣中包含一條字符串“bfce”的路徑(路徑中的字母用下划線標出)。但矩陣中不包含字符串“abfb”的路徑,因為字符串的第一個字符b占據了矩陣中的第一行第二個格子之后,路徑不能再次進入這個格子。
A B T G
C F C S
J D E H
思路
首先對所整個矩陣遍歷,找到第一個字符,然后向上下左右查找下一個字符,由於每個字符都是相同的判斷方法(先判斷當前字符是否相等,再向四周查找),因此采用遞歸函數。由於字符查找過后不能重復進入,所以還要定義一個與字符矩陣大小相同的布爾值矩陣,進入過的格子標記為true。如果不滿足的情況下,需要進行回溯,此時,要將當前位置的布爾值標記回false。(所謂的回溯無非就是對使用過的字符進行標記和處理后的去標記)
測試用例
1.功能測試(多行多列矩陣中存在或者不存在路徑)
2.邊界值測試(矩陣只有一行;矩陣與路徑的所有字符都相同)
3.特殊輸入測試(null)
完整Java代碼
(含測試代碼)
/** * * @Description 面試題12:矩陣中的路徑 * * @author yongh * @date 2018年9月16日 上午11:13:48 */ // 題目:請設計一個函數,用來判斷在一個矩陣中是否存在一條包含某字符串所有 // 字符的路徑。路徑可以從矩陣中任意一格開始,每一步可以在矩陣中向左、右、 // 上、下移動一格。如果一條路徑經過了矩陣的某一格,那么該路徑不能再次進入 // 該格子。例如在下面的3×4的矩陣中包含一條字符串“bfce”的路徑(路徑中的字 // 母用下划線標出)。但矩陣中不包含字符串“abfb”的路徑,因為字符串的第一個 // 字符b占據了矩陣中的第一行第二個格子之后,路徑不能再次進入這個格子。 // A B T G // C F C S // J D E H public class StringPathInMatrix { public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { if (matrix == null || rows < 1 || cols < 1 || str == null) { return false; } boolean[] isVisited = new boolean[rows * cols]; for (boolean v : isVisited) { v = false; } int pathLength = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (hasPathCore(matrix, rows, cols, row, col, str, pathLength, isVisited)) return true; } } return false; } private boolean hasPathCore(char[] matrix, int rows, int cols, int row, int col, char[] str, int pathLength, boolean[] isVisited) { if (row < 0 || col < 0 || row >= rows || col >= cols || isVisited[row * cols + col] == true || str[pathLength] != matrix[row * cols + col]) return false; if (pathLength == str.length - 1) return true; boolean hasPath = false; isVisited[row * cols + col] = true; hasPath = hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength + 1, isVisited) || hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength + 1, isVisited) || hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength + 1, isVisited) || hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength + 1, isVisited); if (!hasPath) { isVisited[row * cols + col] = false; } return hasPath; } // =======測試代碼======== // A B T G // C F C S // J D E H // BFCTB public void test1() { char[] matrix = "ABTGCFCSJDEH".toCharArray(); int rows = 3; int cols = 4; char[] str = "BFCTB".toCharArray(); if (!hasPath(matrix, rows, cols, str)) System.out.println("Test1 passed."); else System.out.println("Test1 failed."); } // A B T G // C F C S // J D E H // BFCE public void test2() { char[] matrix = "ABTGCFCSJDEH".toCharArray(); int rows = 3; int cols = 4; char[] str = "BFCE".toCharArray(); if (hasPath(matrix, rows, cols, str)) System.out.println("Test2 passed."); else System.out.println("Test2 failed."); } // matrix=null public void test3() { char[] matrix = null; int rows = 0; int cols = 0; char[] str = "BFCE".toCharArray(); if (!hasPath(matrix, rows, cols, str)) System.out.println("Test3 passed."); else System.out.println("Test3 failed."); } // str=null public void test4() { char[] matrix = "ABTGCFCSJDEH".toCharArray(); int rows = 3; int cols = 4; char[] str = null; if (!hasPath(matrix, rows, cols, str)) System.out.println("Test4 passed."); else System.out.println("Test4 failed."); } // A // A public void test5() { char[] matrix = "A".toCharArray(); int rows = 1; int cols = 1; char[] str = "A".toCharArray(); if (hasPath(matrix, rows, cols, str)) System.out.println("Test5 passed."); else System.out.println("Test5 failed."); } // A // B public void test6() { char[] matrix = "A".toCharArray(); int rows = 1; int cols = 1; char[] str = "B".toCharArray(); if (!hasPath(matrix, rows, cols, str)) System.out.println("Test6 passed."); else System.out.println("Test6 failed."); } // AAAA // AAAA // AAAA // AAAAAAAAAAAA public void test7() { char[] matrix = "AAAAAAAAAAAA".toCharArray(); int rows = 3; int cols = 4; char[] str = "AAAAAAAAAAAA".toCharArray(); if (hasPath(matrix, rows, cols, str)) System.out.println("Test7 passed."); else System.out.println("Test7 failed."); } public static void main(String[] args) { StringPathInMatrix demo = new StringPathInMatrix(); demo.test1(); demo.test2(); demo.test3(); demo.test4(); demo.test5(); demo.test6(); demo.test7(); } }

Test1 passed.
Test2 passed.
Test3 passed.
Test4 passed.
Test5 passed.
Test6 passed.
Test7 passed.
收獲
1.回溯法用於多個步驟,每個步驟有多個選項的問題:若當前步驟滿足條件,給定一個標記,當發現之后的步驟不滿足條件時,去除標記。
2.字符串轉為字符數組,使用toCharArray()方法
3.二維數組下標的計算:row*cols+col,小心別搞錯。