1. 程式人生 > >《劍指offer》第九_一題(矩陣中的路徑)

《劍指offer》第九_一題(矩陣中的路徑)

pri 退回 span () urn print === true times

// 面試題:矩陣中的路徑
// 題目:請設計一個函數,用來判斷在一個矩陣中是否存在一條包含某字符串所有
// 字符的路徑。路徑可以從矩陣中任意一格開始,每一步可以在矩陣中向左、右、
// 上、下移動一格。如果一條路徑經過了矩陣的某一格,那麽該路徑不能再次進入
// 該格子。例如在下面的3×4的矩陣中包含一條字符串“bfce”的路徑(路徑中的字
// 母用下劃線標出)。但矩陣中不包含字符串“abfb”的路徑,因為字符串的第一個
// 字符b占據了矩陣中的第一行第二個格子之後,路徑不能再次進入這個格子。
// A B T G
// C F C S
// J D E H
#include <string> using namespace std; bool hasPathCore(const char* matrix, int rows, int cols, int row, int col, const char* str, int& pathLength, bool* visited); bool hasPath(const char* matrix, int rows, int cols, const char* str) { if (matrix == nullptr || rows < 1
|| cols < 1 || str == nullptr)//負面測試 return false; bool *visited = new bool[rows * cols];//用來檢測某個節點是否已經走過了 memset(visited, 0, rows * cols);//將開辟出來的visitedp[rows * cols]裏都設為0 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, visited))//遍歷每個點,作為起點,開始檢測 { delete[] visited; return true; } } } delete[] visited; return false; } bool hasPathCore(const char* matrix, int rows, int cols, int row, int col, const char* str, int& pathLength, bool* visited) { if (str[pathLength] == \0)//遞歸停止條件一,我經找到最後一個字符了,查找完畢,返回正確 return true; bool hasPath = false; if (row >= 0 && row < rows && col >= 0 && col < cols && matrix[row * cols + col] == str[pathLength] && !visited[row * cols + col])//這個是我沒有走過的節點中,那符合我現在查找的字符A嗎 { ++pathLength;//符合,那找下個字符B visited[row * cols + col] = true;//這個點在這個起點的時候,我走過了 hasPath = hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength, visited) || hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength, visited) || hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength, visited) || hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength, visited);//以上下左右節點分別為起點檢測字符B if (!hasPath)//完了走不通 { --pathLength;//這個節點A以後的沒希望了,那我退回一下,繼續找A visited[row * cols + col] = false;//那當我這個點沒走過吧 } } return hasPath; } // ====================測試代碼==================== void Test(const char* testName, const char* matrix, int rows, int cols, const char* str, bool expected) { if (testName != nullptr) printf("%s begins: ", testName); if (hasPath(matrix, rows, cols, str) == expected) printf("Passed.\n"); else printf("FAILED.\n"); } //ABTG //CFCS //JDEH //BFCE void Test1() { const char* matrix = "ABTGCFCSJDEH"; const char* str = "BFCE"; Test("Test1", (const char*)matrix, 3, 4, str, true); } //ABCE //SFCS //ADEE //SEE void Test2() { const char* matrix = "ABCESFCSADEE"; const char* str = "SEE"; Test("Test2", (const char*)matrix, 3, 4, str, true); } //ABTG //CFCS //JDEH //ABFB void Test3() { const char* matrix = "ABTGCFCSJDEH"; const char* str = "ABFB"; Test("Test3", (const char*)matrix, 3, 4, str, false); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SLHECCEIDEJFGGFIE void Test4() { const char* matrix = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; const char* str = "SLHECCEIDEJFGGFIE"; Test("Test4", (const char*)matrix, 5, 8, str, true); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SGGFIECVAASABCEHJIGQEM void Test5() { const char* matrix = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; const char* str = "SGGFIECVAASABCEHJIGQEM"; Test("Test5", (const char*)matrix, 5, 8, str, true); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SGGFIECVAASABCEEJIGOEM void Test6() { const char* matrix = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; const char* str = "SGGFIECVAASABCEEJIGOEM"; Test("Test6", (const char*)matrix, 5, 8, str, false); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SGGFIECVAASABCEHJIGQEMS void Test7() { const char* matrix = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; const char* str = "SGGFIECVAASABCEHJIGQEMS"; Test("Test7", (const char*)matrix, 5, 8, str, false); } //AAAA //AAAA //AAAA //AAAAAAAAAAAA void Test8() { const char* matrix = "AAAAAAAAAAAA"; const char* str = "AAAAAAAAAAAA"; Test("Test8", (const char*)matrix, 3, 4, str, true); } //AAAA //AAAA //AAAA //AAAAAAAAAAAAA void Test9() { const char* matrix = "AAAAAAAAAAAA"; const char* str = "AAAAAAAAAAAAA"; Test("Test9", (const char*)matrix, 3, 4, str, false); } //A //A void Test10() { const char* matrix = "A"; const char* str = "A"; Test("Test10", (const char*)matrix, 1, 1, str, true); } //A //B void Test11() { const char* matrix = "A"; const char* str = "B"; Test("Test11", (const char*)matrix, 1, 1, str, false); } void Test12() { Test("Test12", nullptr, 0, 0, nullptr, false); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); Test8(); Test9(); Test10(); Test11(); Test12(); system("pause"); return 0; }

《劍指offer》第九_一題(矩陣中的路徑)