Topic: Please design a function to determine whether there is a path in a matrix that contains all the characters of a string. The path can start from any grid in the matrix, and each step can move one grid to the left, right, top, and bottom in the middle of the matrix. If a path passes through one of the squares of the matrix, then the path cannot enter the grid again.
For example, for example, in the following 3*4 matrix, the path to a string "bcced" is included. However, the matrix does not contain the path of the string "ABCB" because the first character B of the string occupies the first row in the matrix, and the path does not enter the grid again.
A b c E
s f C S
A d e E
The code implemented by backtracking method:
Determine if there is a path to bool Haspath (char* matrix, int rows, int cols, char* str) {if (Matrix = NULL | | Rows < 1 | | Cols < 1 | |
str = NULL) return false;
BOOL *visited = new bool[rows * cols];
memset (visited, 0, rows * cols);
int pathlength = 0; for (int row = 0; row < rows; ++row) {for (int col = 0; col < cols; ++col) {if hasPa Thcore (Matrix, rows, cols, row, col, str, pathlength, visited)) {return true
;
}} delete[] visited;
return false;
BOOL Haspathcore (char* matrix, int rows, int cols, int row, int col, char* str, int& pathlength, bool* visited) {
if (str[pathlength] = = ' = ') return true;
BOOL Haspath = false; if (row >= 0 && row < rows && Col >= 0 && Col < cols && Matrix[row * cols + col] = = Str[pathlength] &&!visited[row * cols + col]) {++pathlength;
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);
if (!haspath) {--pathlength;
Visited[row * cols + col] = false;
} return Haspath;
} void Test (char* testname, char* matrix, int rows, int cols, char* str, bool expected) {if (testname!= NULL)
printf ("%s begins:", testname);
if (Haspath (Matrix, rows, cols, str) = = expected) printf ("passed.\n");
else printf ("failed.\n"); }//ABCE//sfcs//adee//abcced void Test1 () {char matrix[] = "Abcesfcsadee";
char* str = "abcced";
Test ("Test1", (char*) matrix, 3, 4, str, true);
}//ABCE//sfcs//adee//see void Test2 () {char matrix[] = "Abcesfcsadee";
char* str = "the";
Test ("Test2", (char*) matrix, 3, 4, str, true);
}//ABCE//sfcs//adee//ABCB void Test3 () {char matrix[] = "Abcesfcsadee";
char* str = "ABCB";
Test ("Test3", (char*) matrix, 3, 4, str, FALSE); }//abcehjig//sfcslopq//adeemnoe//adidejfm//vceifggs//slhecceidejfggfie void Test4 () {char matrix[] = "ABCEHJI
Gsfcslopqadeemnoeadidejfmvceifggs ";
char* str = "Slhecceidejfggfie";
Test ("Test4", (char*) matrix, 5, 8, str, true); }//abcehjig//sfcslopq//adeemnoe//adidejfm//vceifggs//sggfiecvaasabcehjigqem void Test5 () {char matrix[] = "AB
Cehjigsfcslopqadeemnoeadidejfmvceifggs ";
char* str = "Sggfiecvaasabcehjigqem";
Test ("Test5", (char*) matrix, 5, 8, str, true); }//abcehjig//SFCSLOPQ//adeemnoe//adidEJFM//vceifggs//sggfiecvaasabceejigoem void Test6 () {char matrix[] = "Abcehjigsfcslopqadeemnoeadidejfmvceifggs";
char* str = "Sggfiecvaasabceejigoem";
Test ("Test6", (char*) matrix, 5, 8, str, FALSE); }//abcehjig//sfcslopq//adeemnoe//adidejfm//vceifggs//sggfiecvaasabcehjigqems void Test7 () {char matrix[] = "A
Bcehjigsfcslopqadeemnoeadidejfmvceifggs ";
char* str = "Sggfiecvaasabcehjigqems";
Test ("Test7", (char*) matrix, 5, 8, str, FALSE);
}//aaaa//aaaa//aaaa//aaaaaaaaaaaa void Test8 () {char matrix[] = "AAAAAAAAAAAA";
char* str = "AAAAAAAAAAAA";
Test ("Test8", (char*) matrix, 3, 4, str, true);
}//aaaa//aaaa//aaaa//aaaaaaaaaaaaa void Test9 () {char matrix[] = "AAAAAAAAAAAA";
char* str = "AAAAAAAAAAAAA";
Test ("Test9", (char*) matrix, 3, 4, str, FALSE);
}//a//a void Test10 () {char matrix[] = "A";
char* str = "A";
Test ("Test10", (char*) matrix, 1, 1, str, true); }//a//b void Test11 () {char matrix[] = "A";
char* str = "B";
Test ("Test11", (char*) matrix, 1, 1, str, false);
int main (int argc, char* argv[]) {Test1 ();
Test2 ();
Test3 ();
Test4 ();
Test5 ();
Test6 ();
Test7 ();
Test8 ();
Test9 ();
Test10 ();
Test11 ();
return 0; }