LeetCode-79. Word Searchhttps://leetcode.com/problems/word-search/
Given an m x n
grid of characters board
and a string word
, return true
if word
exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example 1:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" Output: true
Example 2:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE" Output: true
Example 3:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB" Output: false
Constraints:
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board
and word
consists of only lowercase and uppercase English letters.Follow up: Could you use search pruning to make your solution faster with a larger board
?
【C++】
class Solution {
public:
bool exist(vector>& board, string word) {
if (board.empty()) {return false;}
int m = board.size(), n = board[0].size();
vector> visited(m, vector(n, false));
bool find = false;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
backtracking(i, j, board, word, find, visited, 0);
}
}
return find;
}
void backtracking(int i, int j, vector>& board, string& word,
bool& find, vector>& visited, int pos) {
if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size()) {
return;
}
if (visited[i][j] || find || board[i][j] != word[pos]) {
return;
}
if (pos == word.size() - 1) {
find = true;
return;
}
visited[i][j] = true;
backtracking(i + 1, j, board, word, find, visited, pos + 1);
backtracking(i - 1, j, board, word, find, visited, pos + 1);
backtracking(i, j + 1, board, word, find, visited, pos + 1);
backtracking(i, j - 1, board, word, find, visited, pos + 1);
visited[i][j] = false;
}
};
find的bool值不引用亦可,但会有部分回溯在find后仍在继续执行。
class Solution {
public:
bool exist(vector>& board, string word) {
if (board.empty()) {return false;}
int m = board.size(), n = board[0].size();
vector> visited(m, vector(n, false));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if(backtracking(i, j, board, word, visited, 0)) {return true;}
}
}
return false;
}
bool backtracking(int i, int j, vector>& board, string& word,
vector>& visited, int pos) {
if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size()) {
return false;
}
if (visited[i][j] || board[i][j] != word[pos]) {
return false;
}
if (pos == word.size() - 1) {
return true;
}
visited[i][j] = true;
bool find = backtracking(i + 1, j, board, word, visited, pos + 1)
|| backtracking(i - 1, j, board, word, visited, pos + 1)
|| backtracking(i, j + 1, board, word, visited, pos + 1)
|| backtracking(i, j - 1, board, word, visited, pos + 1);
visited[i][j] = false;
return find;
}
};
【Java】
class Solution {
public boolean exist(char[][] board, String word) {
if (board.length == 0 || board[0].length == 0) {return false;}
int m = board.length, n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if(backtracking(i, j, board, word, visited, 0)) {return true;}
}
}
return false;
}
boolean backtracking(int i, int j, char[][] board, String word,
boolean[][] visited, int pos) {
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) {
return false;
}
if (visited[i][j] || board[i][j] != word.charAt(pos)) {
return false;
}
if (pos == word.length() - 1) {
return true;
}
visited[i][j] = true;
boolean find = backtracking(i + 1, j, board, word, visited, pos + 1)
|| backtracking(i - 1, j, board, word, visited, pos + 1)
|| backtracking(i, j + 1, board, word, visited, pos + 1)
|| backtracking(i, j - 1, board, word, visited, pos + 1);
visited[i][j] = false;
return find;
}
}