Topic links: https://leetcode-cn.com/problems/word-search/
Subject description:
Given a two-dimensional grid and a word, to find out whether the word exists in the grid.
Words must, by the letters in adjacent cells constituting alphabetically, where "adjacent" cells that are adjacent horizontally or vertically adjacent cells. Letters within the same cell is not allowed to be reused.
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.
Ideas:
Backtracking algorithms, direct look at the code, it is easy to understand
Code:
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
row = len(board)
col = len(board[0])
def helper(i, j, k, visited):
#print(i,j, k,visited)
if k == len(word):
return True
for x, y in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
tmp_i = x + i
tmp_j = y + j
if 0 <= tmp_i < row and 0 <= tmp_j < col and (tmp_i, tmp_j) not in visited \
and board[tmp_i][tmp_j] == word[k]:
visited.add((tmp_i, tmp_j))
if helper(tmp_i, tmp_j, k+1, visited):
return True
visited.remove((tmp_i, tmp_j)) # 回溯
return False
for i in range(row):
for j in range(col):
if board[i][j] == word[0] and helper(i, j, 1,{(i, j)}) :
return True
return False
java
class Solution {
public boolean exist(char[][] board, String word) {
boolean[][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (word.charAt(0) == board[i][j] && backtrack(i, j, 0, word, visited, board)) return true;
}
}
return false;
}
private boolean backtrack(int i, int j, int idx, String word, boolean[][] visited, char[][] board) {
if (idx == word.length()) return true;
if (i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word.charAt(idx) || visited[i][j])
return false;
visited[i][j] = true;
if (backtrack(i + 1, j, idx + 1, word, visited, board) || backtrack(i - 1, j, idx + 1, word, visited, board) || backtrack(i, j + 1, idx + 1, word, visited, board) || backtrack(i, j - 1, idx + 1, word, visited, board))
return true;
visited[i][j] = false; // 回溯
return false;
}
}