Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example:
board =
[
[‘A’,’B’,’C’,’E’],
[‘S’,’F’,’C’,’S’],
[‘A’,’D’,’E’,’E’]
]
Given word = “ABCCED”, return true.
Given word = “SEE”, return true.
Given word = “ABCB”, return false.
Typical dfs+backtracking question. It compare board[row][col] with word[start], if they match, change board[row][col] to ‘*’ to mark it as visited. Then move to the next one (i.e. word[start+1]) and compare it to the current neighbors ( doing it by recursion)
dfs+回溯:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class Solution {
private:
bool dfs(vector<vector<char>>& board, int row, int col, const string &word, int start, int M, int N, int sLen)
{
char curC;
bool res = false;
if( (curC = board[row][col]) != word[start]) return false;
if(start==sLen-1) return true;
board[row][col] = '*';
if(row>0) res = dfs(board, row-1, col, word, start+1, M, N, sLen);
if(!res && row < M-1) res = dfs(board, row+1, col, word, start+1, M, N, sLen);
if(!res && col > 0) res = dfs(board, row, col-1, word, start+1, M, N, sLen);
if(!res && col < N-1) res = dfs(board, row, col+1, word, start+1, M, N, sLen);
board[row][col] = curC;
return res;
}
public:
bool exist(vector<vector<char>>& board, string word) {
int M,N,i,j,sLen = word.size();
if( (M=board.size()) && (N=board[0].size()) && sLen)
{
for(i=0; i<M; ++i)
for(j=0; j<N; ++j)
if(dfs(board, i, j, word, 0, M, N, sLen)) return true;
}
return false;
}
};