第65题 矩阵中的路径
题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如
[
a
b
c
e
s
f
c
s
a
d
e
e
]
\begin{bmatrix} a & b & c &e \\ s & f & c & s \\ a & d & e& e\\ \end{bmatrix}\quad
⎣⎡asabfdcceese⎦⎤
矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
思路:
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param matrix string字符串
* @param rows int整型
* @param cols int整型
* @param str string字符串
* @return bool布尔型
*/
bool hasPath(string matrix, int rows, int cols, string str) {
vector<bool> visited(rows*cols,false);
for(int i = 0;i < rows; ++i){
for(int j = 0; j < cols; ++j){
if(judge(matrix, visited, str, rows, cols, i, j, 0)) return true;
}
}
return false;
}
bool judge(string matrix, vector<bool> & visited, string str,int rows, int cols, int i, int j, int k){
int is_visiting_id = i*cols + j;
if(i<0 || i>=rows || j<0 || j>cols || visited[is_visiting_id] || matrix[is_visiting_id] != str[k]){
return false;
}
visited[is_visiting_id] = true;
if(k == str.length()-1) return true;
if(judge(matrix,visited,str,rows,cols,i+1,j,k+1)||judge(matrix,visited,str,rows,cols,i-1,j,k+1)||judge(matrix,visited,str,rows,cols,i,j+1,k+1)||judge(matrix,visited,str,rows,cols,i,j-1,k+1)){
return true;
}
visited[is_visiting_id] = false;
return false;
}
};
class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
if not board:
return False
def dfs(i,j,s):
if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]) or s >=len(word) or board[i][j] != word[s]:
return False
board[i][j] = ''
if s == len(word) - 1:
return True
flag = dfs(i+1,j,s+1) or dfs(i-1,j,s+1) or dfs(i,j+1,s+1) or dfs(i,j-1,s+1)
board[i][j] = word[s]
return flag
for i in range(len(board)):
for j in range(len(board[0])):
if dfs(i,j,0):
return True
return False
judge函数进行递归判断,如果出界或已访问或不为当前字母时,返回false,否则访问数组记当前位置为true表示已访问,然后递归进行下一个字母和位置(k表示str遍历位置),每次递归回溯时,置当前位置访问数组为false再退回。
类似题目
对于一个矩阵,将非0连通区域置值为1,矩阵如图所示,下半部分为连通区域,上面有非0的非连通区域
[
2
0
0
0
0
2
0
1
3
]
\begin{bmatrix} 2 & 0 & 0 \\ 0 & 0& 2\\ 0 & 1 & 3\\ \end{bmatrix}\quad
⎣⎡200001023⎦⎤
目标将其置为:
[
2
0
0
0
0
1
0
1
1
]
\begin{bmatrix} 2 & 0 & 0 \\ 0 & 0& 1\\ 0 & 1 & 1\\ \end{bmatrix}\quad
⎣⎡200001011⎦⎤
#include <iostream>
#include <vector>
using namespace std;
void judge(vector<vector<int>>& matrix, vector<vector<bool>>& vis, int i, int j){
if (i<0 || i>= matrix.size() || j<0 || j>= matrix[0].size() || vis[i][j] || matrix[i][j] == 0) {
return;
}
vis[i][j] = true;
judge(matrix, vis, i + 1, j);
judge(matrix, vis, i - 1, j);
judge(matrix, vis, i, j + 1);
judge(matrix, vis, i, j-1);
matrix[i][j] = 1;
vis[i][j] = false;
return;
}
bool judgel(vector<vector<int>>& matrix, vector<vector<bool>>& vis, int i, int j, int k) {
if (i < 0 || i >= matrix.size() || j < 0 || j >= matrix[0].size() || matrix[i][j] == 0) {
return false;
}
if (vis[i][j]) return true;
vis[i][j] = true;
if (judgel(matrix,vis, i + 1, j, k + 1) | judgel(matrix,vis, i - 1, j, k + 1) | judgel(matrix,vis, i, j + 1, k + 1) | judgel(matrix,vis, i, j - 1, k + 1) || k != 0) {
matrix[i][j] = 1;
return true;
}
vis[i][j] = false;
return false;
}
int main()
{
vector<vector<int>> mat = { {2,0,0},{0,0,2},{0,1,3} };
vector<vector<bool>> vis = { {false,false,false},{false,false,false},{false,false,false}};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
judgel(mat, vis, i, j,0);
}
}
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << mat[i][j];;
}
cout << endl;
}
cout << endl;
cout << "Hello World";
return 0;
}
思路:
使用judgel函数递归,vis保存已访问防止重复访问无限循环,返回bool值用于判断是否连通。