"topic description" 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 lattice in the matrix, and each step can be left in the matrix, right, up, and down by a lattice. If a path passes through a lattice in the matrix, the path can no longer enter the grid. For example a B c e s f C s a D e e matrix contains a string "bcced" path, however, the matrix does not contain a "ABCB" path, because the first character B of the string occupies the first row in the matrix after the second lattice, the path can not enter the grid again.
"Ideas for solving problems"
This is a typical problem that can be solved by using the back-up method.
1. First, select a lattice in the matrix as the starting point for the path. If the first character on the path is not CH, then the lattice cannot be positioned at the first position on the path. If the first character on the path happens to be ch, look for the first i+1 character on the path to the adjacent grid. In addition to the lattice on the boundary of the Matrix, the other lattices have 4 adjacent lattices. Repeat this process until all the characters on the path find the appropriate position in the matrix.
2. The path can be turned into a stack because of the recursive properties of the back-up method. When the position of the first n characters in the path is positioned in the matrix, the n+1 character is not found around the grid corresponding to the nth character, and the nth character is repositioned as long as the n-1 character is returned on the path.
3. Since the path cannot be repeated into the lattice of the Matrix, a Boolean matrix of the same size as the character matrix is needed to identify whether the path has entered each lattice. Locate the next character in the path string from 4 adjacent lattices (ROW,COL-1), (Row-1,col), (row,col+1), and (Row+1,col) when the corresponding characters in the Row,col lattice and the path string are the same in the matrix.
4. If 4 adjacent squares do not match the next character in the string, indicating that the characters in the current path string are not positioned correctly in the matrix, we need to go back to the previous one and reposition it.
5. Repeat this process until all the characters in the path string find the right place in the matrix
public class Solution {public boolean haspath (char[] matrix, int rows, int cols, char[] str) {if (matrix= =null | | rows<=0 | | cols<=0 | |
Str==null) {return false;
} if (str.length==0) {return true;
} boolean[] visited = new Boolean[matrix.length]; for (int i=0; i<rows; i++) {for (int j=0; j<cols; J + +) {if Findpath (matrix, I, J, rows, CO
LS, 0, visited, str)) {return true;
}} return false; //try to find the path public boolean findpath (char[] Matrix, int row, int col, int rows, int cols, int k, boolean[] visited, Char[] str) {if (row<0 | | row>=rows | | col<0 | | col>=cols | | str[k]!=matrix[row*cols+col] | | visited[r
Ow*cols+col]) {return false;
} if (K==str.length-1) {return true;
} Visited[row*cols+col] = true; if (Findpath (mAtrix, row+1, col, rows, cols, k+1, visited, str) | | Findpath (Matrix, Row, col+1, rows, cols, k+1, visited, str) | Findpath (Matrix, row-1, col, rows, cols, k+1, visited, str) |
Findpath (Matrix, Row, col-1, rows, cols, k+1, visited, str)) {return true;
} Visited[row*cols+col] = false;
return false; }
}
Solving Ideas 2
//using a stack and an array to assist.
//1, traversing the entire array of strings and Poute the word that matches the first STR character to the stack (possibly multiple)
//2, out of the stack, will be out of the stack of the subscript element has been visited to find the next character with no str around it, if any, all into the stack;
//3, Returns true until all str characters are found,
//4, or STR is not found, the stack is empty, and returns to Fasle.
Import Java.util.Stack; public class Solution {public boolean haspath (char[] matrix, int rows, int cols, char[] str) {if (matrix= =null| | str = = null| |
Matrix.length==0) return false;
if (Str.length = = 0) return true;
Boolean result = false;
stack<integer> stack = new stack<integer> ();
Boolean[] visited = new Boolean[rows*cols];
for (int i = 0; i < matrix.length i++) {if (matrix[i]==str[0)) {Stack.push (i);
result = true; for (int j = 1;!)
Stack.empty () &&j<str.length;) {result = false;
int index = Stack.pop ();
Visited[index] = true;
if (Hasnext (Matrix, rows, cols, index, Stack, str[j], visited)) {if (++j = = str.length) result = true;
} return result; ///Find the next element in the perimeter of a subscript that has no str, public boolean hasnext (char[] matrix,int rows,int cols,int index,stack<integer> Stack,char c,boolean[] visited) {int up = Index-cols;
int down = Index+cols;
int left = index-1;
int right = index+1;
Boolean hasnext = false;
if (up>=0&&matrix[up] = = C&&!visited[up]) {Stack.push (UP);
Hasnext = true;
} if (down<matrix.length&&matrix[down] = = C&&!visited[down]) {Stack.push (down);
Hasnext = true;
} if (Index%cols!= 0&&matrix[left] = = C&&!visited[left]) {Stack.push (left);
Hasnext = true;
} if (Right%cols!= 0&&matrix[right] = = C&&!visited[right]) {Stack.push (right);
Hasnext = true;
return hasnext; }
}