017 矩阵中的路径
生活随笔
收集整理的這篇文章主要介紹了
017 矩阵中的路径
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.題目
請設計一個函數,用來判斷在一個矩陣中是否存在一條包含某字符串所有字符的路徑。
路徑可以從矩陣中的任意一個格子開始,每一步可以在矩陣中向左,向右,向上,向下移動一個格子。
如果一條路徑經過了矩陣中的某一個格子,則之后不能再次進入這個格子。
例如 a b c e s f c s a d e e 這樣的3 X 4 矩陣中包含一條字符串"bcced"的路徑,但是矩陣中不包含"abcb"路徑,因為字符串的第一個字符b占據了矩陣中的第一行第二個格子之后,路徑不能再次進入該格子。
?
2.思路
分析:回溯算法 這是一個可以用回朔法解決的典型題。首先,在矩陣中任選一個格子作為路徑的起點。如果路徑上的第i個字符不是ch,那么這個格子不可能處在路徑上的第i個位置。 如果路徑上的第i個字符正好是ch,那么往相鄰的格子尋找路徑上的第i+1個字符。除在矩陣邊界上的格子之外,其他格子都有4個相鄰的格子。重復這個過程直到路徑上的所有字符都在矩陣中找到相應的位置。 由于回朔法的遞歸特性,路徑可以被開成一個棧。當在矩陣中定位了路徑中前n個字符的位置之后,在與第n個字符對應的格子的周圍都沒有找到第n+1個字符,這個時候只要在路徑上回到第n-1個字符,重新定位第n個字符。 由于路徑不能重復進入矩陣的格子,還需要定義和字符矩陣大小一樣的布爾值矩陣,用來標識路徑是否已經進入每個格子。 當矩陣中坐標為(row,col)的格子和路徑字符串中相應的字符一樣時,從4個相鄰的格子(row,col-1),(row-1,col),(row,col+1)以及(row+1,col)中去定位路徑字符串中下一個字符 如果4個相鄰的格子都沒有匹配字符串中下一個的字符,表明當前路徑字符串中字符在矩陣中的定位不正確,我們需要回到前一個,然后重新定位。 一直重復這個過程,直到路徑字符串上所有字符都在矩陣中找到合適的位置。 3.程序 1 package first; 2 3 public class HasPath { 4 public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { 5 if (matrix == null || rows < 1 || cols < 1 || str == null) { 6 return false; 7 } 8 boolean[] isVisited = new boolean[rows * cols]; 9 for (boolean v : isVisited) { 10 v = false; 11 } 12 int pathLength = 0; 13 for (int row = 0; row < rows; row++) { 14 for (int col = 0; col < cols; col++) { 15 if (hasPathCore(matrix, rows, cols, row, col, str, pathLength, isVisited)) 16 return true; 17 } 18 } 19 return false; 20 } 21 22 private boolean hasPathCore(char[] matrix, int rows, int cols, int row, int col, char[] str, int pathLength, boolean[] isVisited) { 23 if (row < 0 || col < 0 || row >= rows || col >= cols || isVisited[row * cols + col] == true || str[pathLength] != matrix[row * cols + col]) 24 return false; 25 if (pathLength == str.length - 1) 26 return true; 27 boolean hasPath = false; 28 isVisited[row * cols + col] = true; 29 hasPath = hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength + 1, isVisited) 30 || hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength + 1, isVisited) 31 || hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength + 1, isVisited) 32 || hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength + 1, isVisited); 33 34 if (!hasPath) { 35 isVisited[row * cols + col] = false; 36 } 37 return hasPath; 38 } 39 40 // =======測試代碼======== 41 42 // A B T G 43 // C F C S 44 // J D E H 45 46 // BFCTB 47 public void test1() { 48 char[] matrix = "ABTGCFCSJDEH".toCharArray(); 49 int rows = 3; 50 int cols = 4; 51 char[] str = "BFCTB".toCharArray(); 52 if (!hasPath(matrix, rows, cols, str)) 53 System.out.println("Test1 passed."); 54 else 55 System.out.println("Test1 failed."); 56 } 57 58 // A B T G 59 // C F C S 60 // J D E H 61 62 // BFCE 63 public void test2() { 64 char[] matrix = "ABTGCFCSJDEH".toCharArray(); 65 int rows = 3; 66 int cols = 4; 67 char[] str = "BFCE".toCharArray(); 68 if (hasPath(matrix, rows, cols, str)) 69 System.out.println("Test2 passed."); 70 else 71 System.out.println("Test2 failed."); 72 } 73 74 // matrix=null 75 public void test3() { 76 char[] matrix = null; 77 int rows = 0; 78 int cols = 0; 79 char[] str = "BFCE".toCharArray(); 80 if (!hasPath(matrix, rows, cols, str)) 81 System.out.println("Test3 passed."); 82 else 83 System.out.println("Test3 failed."); 84 } 85 86 // str=null 87 public void test4() { 88 char[] matrix = "ABTGCFCSJDEH".toCharArray(); 89 int rows = 3; 90 int cols = 4; 91 char[] str = null; 92 if (!hasPath(matrix, rows, cols, str)) 93 System.out.println("Test4 passed."); 94 else 95 System.out.println("Test4 failed."); 96 } 97 98 // A 99 100 // A 101 public void test5() { 102 char[] matrix = "A".toCharArray(); 103 int rows = 1; 104 int cols = 1; 105 char[] str = "A".toCharArray(); 106 if (hasPath(matrix, rows, cols, str)) 107 System.out.println("Test5 passed."); 108 else 109 System.out.println("Test5 failed."); 110 } 111 112 // A 113 114 // B 115 public void test6() { 116 char[] matrix = "A".toCharArray(); 117 int rows = 1; 118 int cols = 1; 119 char[] str = "B".toCharArray(); 120 if (!hasPath(matrix, rows, cols, str)) 121 System.out.println("Test6 passed."); 122 else 123 System.out.println("Test6 failed."); 124 } 125 126 // AAAA 127 // AAAA 128 // AAAA 129 130 // AAAAAAAAAAAA 131 public void test7() { 132 char[] matrix = "AAAAAAAAAAAA".toCharArray(); 133 int rows = 3; 134 int cols = 4; 135 char[] str = "AAAAAAAAAAAA".toCharArray(); 136 if (hasPath(matrix, rows, cols, str)) 137 System.out.println("Test7 passed."); 138 else 139 System.out.println("Test7 failed."); 140 } 141 142 public static void main(String[] args) { 143 HasPath demo = new HasPath(); 144 demo.test1(); 145 // demo.test2(); 146 // demo.test3(); 147 // demo.test4(); 148 // demo.test5(); 149 // demo.test6(); 150 // demo.test7(); 151 } 152 }?
?
轉載于:https://www.cnblogs.com/juncaoit/p/10495733.html
總結
以上是生活随笔為你收集整理的017 矩阵中的路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《剑指offer》第三十一题(栈的压入、
- 下一篇: BZOJ1558 等差数列