题意:给定一个m*n的矩阵,求在矩阵中是否能搜索到这个单词
https://leetcode.com/problems/word-search/description/
Input: board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
Output: true
解答:遇到第一个匹配的开头(比如例子中的A),开始搜索,用dfs来搜索四个方向,并用vis数组确保已经访问过的节点不会被访问,用一个变量来记录我当前搜索到哪一个位置了
class Solution {
public:
bool ret;
bool exist(vector<vector<char>>& board, string word) {
if(!word.size()) {
return true;
}
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(board[i][j] == word[0]) {
vector<vector<int>> vis(m, vector<int>(n,0));
vis[i][j] = 1;
dfs(board, 1, i, j, vis, word);
if(ret) {
return true;
}
}
}
}
return false;
}
void dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>>& vis, string word) {
int m = board.size();
int n = board[0].size();
if(st == word.size()) {
ret = true;
return;
}
int dk[] = {-1, 0, 1, 0, -1};
for(int i = 0; i < 4; i++) {
int dx = x + dk[i];
int dy = y + dk[i+1];
if(dx >= 0 && dx < m && dy >= 0 && dy < n && !vis[dx][dy] && board[dx][dy] == word[st]) {
vis[dx][dy] = 1;
dfs(board, st+1, dx, dy, vis, word);
vis[dx][dy] = 0;
}
}
}
};
question:为什么下面的代码过不了?以及有哪些不好的地方?
class Solution {
public:
bool ret;
bool exist(vector<vector<char>>& board, string word) {
if(!word.size()) {
return true;
}
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(board[i][j] == word[0]) {
vector<vector<int>> vis(m, vector<int>(n,0));
vis[i][j] = 1;
dfs(board, 1, i, j, vis, word);
if(ret) {
return true;
}
}
}
}
return false;
}
void dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>> vis, string word) {
int m = board.size();
int n = board[0].size();
if(st == word.size()) {
ret = true;
return;
}
int dk[] = {-1, 0, 1, 0, -1};
for(int i = 0; i < 4; i++) {
int dx = x + dk[i];
int dy = y + dk[i+1];
if(dx >= 0 && dx < m && dy >= 0 && dy < n && !vis[dx][dy] && board[dx][dy] == word[st]) {
vis[dx][dy] = 1;
dfs(board, st+1, dx, dy, vis, word);
vis[dx][dy] = 0;
}
}
}
};
为什么过不了?因为vis数组产生了copy,这个开销是MN的,你的答案的算法复杂度会变成M2N2
并且这个ret完全没必要,你完全可以通过dfs的返回值消掉这个ret
而且vis数组其实可以写在里面的
question:为什么这个会有问题?因为board[dx][dy] == word[st+1]
会有越界问题的,要想输入dfs(board, 0, i, j, vis, word
,我必须每次进入的时候判定,而不是说每一次for循环中去判定
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if(!word.size()) {
return true;
}
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(board[i][j] == word[0]) {
vector<vector<int>> vis(m, vector<int>(n,0));
if(dfs(board, 0, i, j, vis, word)) {
return true;
}
}
}
}
return false;
}
bool dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>>& vis, string word) {
int m = board.size();
int n = board[0].size();
vis[x][y] = 1;
if(st == word.size()) {
return true;
}
int dk[] = {-1, 0, 1, 0, -1};
for(int i = 0; i < 4; i++) {
int dx = x + dk[i];
int dy = y + dk[i+1];
if(dx >= 0 && dx < m && dy >= 0 && dy < n && !vis[dx][dy] && board[dx][dy] == word[st+1]) {
if(dfs(board, st+1, dx, dy, vis, word)) {
return true;
}
}
}
vis[x][y] = 0;
return false;
}
};
最为简洁的写法,写的时候注意,先判断true,再来false
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if(!word.size()) {
return true;
}
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(board[i][j] == word[0]) {
vector<vector<int>> vis(m, vector<int>(n,0));
if(dfs(board, 0, i, j, vis, word)) {
return true;
}
}
}
}
return false;
}
bool dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>>& vis, string word) {
int m = board.size();
int n = board[0].size();
if(st == word.size()) {
return true;
}
if(x < 0 || x >= m || y < 0 || y >= n) {
return false;
}
if(vis[x][y] || board[x][y] != word[st]) {
return false;
}
vis[x][y] = 1;
int dk[] = {-1, 0, 1, 0, -1};
for(int i = 0; i < 4; i++) {
int dx = x + dk[i];
int dy = y + dk[i+1];
if(dfs(board, st+1, dx, dy, vis, word)) {
return true;
}
}
vis[x][y] = 0;
return false;
}
};
算法复杂度(MN3^L)
MN是因为第一个大循环,第二个dfs相当于搜索长度为L的字符串,有3个方向可以搜索(去除了回退哪一个)