bool hasPath(char* matrix, int rows, int cols,char* str )
{
if(str == NULL||rows<=0||cols<=0)
return false;
bool *isOK = new bool[rows*cols]();
for(int i = 0;i < rows;i++)
{
for(int j = 0;j < cols;i++)
{
if(isHsaPath(matrix,rows,cols,str,isOK,i,j))
return true;
}
return false;
}
}
bool isHsaPath(char* matrix,int rows,int cols,char* str,bool* isOK,int curx,int cury)
{
if(*str == '\0')
return true;
if(cury == cols)
{
curx++;
cury=0;
}
if(curx<0||curx>=rows)
return false;
if(isOK[curx*cols+cury]||*str != matrix[curx*cols+cury])
return false;
isOK[curx*cols+cury] = true;
bool sign = isHsaPath(matrix,rows,cols,str+1,isOK,curx-1,cury)
||isHsaPath(matrix,rows,cols,str+1,isOK,curx+1,cury)
||isHsaPath(matrix,rows,cols,str+1,isOK,curx,cury-1)
||isHsaPath(matrix,rows,cols,str+1,isOK,curx,cury+1);
isOK[curx*cols+cury] = false;
return sign;
}