题目描述:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
思路:回溯法(通过递归)。从为止(0,0)开始,“贪心”判断该点是否匹配*str,如果匹配,则设置该点为访问过,str++,递归其上下左右四个点。
解:class Solution {
private:
bool isPath(char *matrix,vector<char> flags,char* str,int x,int y,int rows, int cols) {
if(x<0 || x>=rows || y<0 || y>=cols) return false;
if( matrix[x*cols+y]== *str && flags[x*cols+y]==0 ){ //匹配并且没被访问过
flags[x*cols+y]=1; //必须先设置成访问,如最终失败再设回未访问
if(*(str+1)=='\0') return true;
//递归 左、上、右、下
bool condition =isPath(matrix,flags,(str+1),x,y-1,rows,cols) ||
isPath(matrix,flags,(str+1),x-1,y,rows,cols)||
isPath(matrix,flags,(str+1),x,y+1,rows,cols)||
isPath(matrix,flags,(str+1),x+1,y,rows,cols);
if(condition == false) flags[x*cols+y]=0;
return condition;
}
else return false;
}
public:
bool hasPath(char* matrix, int rows, int cols, char* str) {
vector<char> flags(rows*cols,0);
bool condition=false;
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++) {
condition= (condition || isPath(matrix,flags,str,i,j,rows,cols) );
}
return condition;
}
};
网友评论