题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
/*
* 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。
* 路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
* 如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子
*
* 思路:回溯法:首先任意一个点都有可能成为起点,所以要获得任意一点的坐标(位于第几行,第几列)
* 其次要有一个数组记录这个点是否被访问过,同时要有一个指针来记录字符串中字符的位置。
* 当某个点成为合法的起点时,即这个点与字符串中第一个字符相等,则可以继续朝上下左右搜索下一个点;
* 如果这个点不能成为合法的起点,则恢复现场(这个点没有被访问过且字符串指针后退)
*/
public class HasPath {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if(matrix.length <= 0 || rows < 0 || cols < 0 || str.length <= 0) {
return false;
}
boolean visited[] = new boolean[rows * cols];
int [] index = {0};
for(int i = 0;i < rows;i ++) {
for(int j = 0;j < cols;j ++) {
if(isPath(matrix, rows, cols, i, j, str, visited, index)) {
return true;
}
}
}
return false;
}
public boolean isPath(char[] matrix, int rows, int cols, int row, int col, char[] str, boolean visited[], int [] index) {
if(index[0] == str.length) {
return true;
}
boolean flag = false;
//当前点折算到原数组的位置是:row * cols + col
if(row >= 0 && row < rows && col >= 0 && col < cols
&& !visited[row * cols + col] && matrix[row * cols + col] == str[index[0]]) {
index[0] ++; //指针右移
visited[row * cols + col] = true;
//第一个点是合法的起点之后开始回溯:上下左右进行搜索
flag = isPath(matrix, rows, cols, row - 1, col, str, visited, index) ||
isPath(matrix, rows, cols, row + 1, col, str, visited, index) ||
isPath(matrix, rows, cols, row, col - 1, str, visited, index) ||
isPath(matrix, rows, cols, row, col + 1, str, visited, index);
if(!flag) { //恢复现场
index[0] --;
visited[row * cols + col] = false;
}
}
return flag;
}
public static void main(String[] args) {
String str = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS";
char[] matrix = str.toCharArray();
String str2 = "SGGFIECVAASABCEEJIGOEM";
char[] c = str2.toCharArray();
System.out.println(new HasPath().hasPath(matrix, 5, 8, c));
}
}
#####################
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;
}
};
添加笔记