Word Search:
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.
Solution:
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
class Solution {
private:
int d[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int m, n;
vector<vector<bool>> visited;
bool inArea(int x, int y){
return x >= 0 && x < m && y >= 0 && y < n;
}
// 从board[startx][starty]开始,寻找word[index...word.size()]
bool searchWord(const vector<vector<char>> &board, const string& word, int index,
int startx, int starty){
if ( index == word.size() - 1)
return board[startx][starty] == word[index];
if ( board[startx][starty] == word[index] ){
visited[startx][starty] = true;
// 从startx, starty出发,向四个方向寻找
for(int i = 0; i < 4; i++){
// 小技巧,这个cell的四个方向
int newx = startx + d[i][0];
int newy = starty + d[i][1];
if( inArea(newx,newy) && !visited[newx][newy] &&
searchWord( board, word, index + 1, newx, newy))
return true;
}
visited[startx][starty] = false;
}
return false;
}
public:
bool exist(vector<vector<char>>& board, string word) {
m = board.size();
assert( m > 0);
n = board[0].size();
visited = vector<vector<bool>>(m, vector<bool>(n, false));
for (int i = 0; i < board.size() ; i++) {
for (int j = 0; j < board[i].size() ; j++) {
if (searchWord(board, word, 0, i, j))
return true;
}
}
return false;
}
};
总结: 二维平面上的回溯问题,值得回味!