题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
思路:
采用回溯法。遍历整个矩阵确定起始点,然后从起始点开始用递归的方法寻找剩下的节点。如果当前位置(row,col,matrix[row*cols+col])的值正好等于字符串中对应位置的的值,那么字符串中往后走一位(pathLength++),在矩阵当前位置的上下左右四个位置继续搜索下一个值,如果没有相等的值,就退回一格(pathLength--),直到到达字符串的结束符,如果前面都没有返回false,就返回true。
注意:
因为规定不能重复进入格子,需要用一个bool数组储存是否走过当前格子,每次走过一个格子,将其对应的bool值设为1,如果回退一个,再设为0,每次只有当格子没有被访问过才能进入。这里不需要用二维数组,直接申请长度为rows*cols的一维动态数组即可。动态数组的初始化可以使用memset(s,ch,n)快速初始化,即用ch来初始化从s位置开始大小为n的一段内存。
参考代码:
在线测试
AC代码
class Solution {
public:
bool hasPath(char* matrix, int rows, int cols, char* str)
{
if(matrix==NULL||rows<=0||cols<=0||str==NULL)//异常处理
return false;
bool* visited=new bool[rows*cols];
memset(visited,0,rows*cols);
int pathLength=0;//对应目标字符串中的位置
//遍历找到起始格子
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
if(hasPathCore(matrix, rows, cols, i, j, str, pathLength, visited))
{
delete[] visited;//找到之后记得释放动态申请的内存空间
return true;
}
}
}
return false;
}
//搜索需要当前位置(row,col),当前对应目标字符串中的位置,以及标识位置是否被访问的数组
bool hasPathCore(char* matrix, int rows, int cols, int row, int col, char* str, int& pathLength, bool* visited)
{
if(str[pathLength]=='\0')
return true; //到达字符串结束符,递归结束,返回true
bool hasPath=false;//要设一个变量作为返回值,无法直接返回表达式
//注意由于下面对row和col有加加减减,所以要判断这个位置是否有效,另外确定当前格子没有访问过,同时当前格子的值与字符串中当前字符相等,才进入判断下一个字符
if(row>=0 && row<rows && col>=0 && col<cols && !visited[row*cols+col] && matrix[row*cols+col]==str[pathLength])
{
visited[row*cols+col]=true;//当前格子已经被访问过
pathLength++;//字符串中往后走一步
if(hasPathCore(matrix, rows, cols, row, col-1, str, pathLength, visited)||
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-1, col, str, pathLength, visited)
hasPath=true;//从四个方向搜索,找到与剩下字符串对应的路径,将hasPath设为true
else//没有找到的hau话,回退一步,注意这个是在大的if条件里面,因为只有先pathLength++又没有找到才进行pathLength--
{
pathLength--;
visited[row*cols+col]=false;
}
}
return hasPath;
}
};
leetcode版本的代码
注意二维vector的初始化,还有当找到满足board[i][j]==word[0]的位置后,不能直接return existCore(board,word,visited,i,j,0),因为可能有多个值一样的位置,而不一定每个位置都有满足条件的路径,如果当前找到的起始点没有,还要继续搜索后面的位置。
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if(board.empty()||word=="")
return false;
int rows=board.size();
int cols=board[0].size();
vector<bool> initial_visited(cols,false);
vector<vector<bool>> visited(rows,initial_visited);
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
if(board[i][j]==word[0])
if(existCore(board,word,visited,i,j,0))
return true;
}
}
return false;
}
bool existCore(vector<vector<char>>& board, string word, vector<vector<bool>>& visited, int row, int col, int start)
{
int rows=board.size();
int cols=board[0].size();
if(start==word.size())
return true;
bool result=false;
if(row<rows && row>=0 && col<cols && col>=0 && !visited[row][col] && board[row][col]==word[start])
{
visited[row][col]=true;
if(existCore(board, word, visited, row-1, col, start+1)||existCore(board, word, visited, row+1, col, start+1)
||existCore(board, word, visited, row, col-1, start+1)||existCore(board, word, visited, row, col+1, start+1))
result=true;
else
visited[row][col]=false;
}
return result;
}
};