1回溯法
class Solution {
public:
bool hasPath(char* matrix, int rows, int cols, char* str)
{
if( matrix==NULL||str==NULL||rows<=0||cols<=0)
return false;
int row=0;
int col=0;
bool * visited=new bool[rows*cols]; //new 一个矩阵,返回一个指针;用以检测每个节点是否经过了的;
memset(visited,0,rows*cols);
int pathlength=0;//字符串所在的位置
for(row=0;row<rows;row++)
{
for(col=0;col<cols;col++)
{
if(hasPathCore(matrix,rows,cols,row,col,str,visited,pathlength))//如果路径搜索成功
{
return true;
}
}
}
delete [] visited;
return false;
}
public :
bool hasPathCore(char* matrix,int rows, int cols, int row, int col, char* str, bool * visited,int & pathlength)
{
bool haspath=false;
//指针的话上面已经验证过非空指针了
if(str[pathlength]=='