标签:visit ack 顺序 单词 exist || ble i++ site
题目链接 : https://leetcode-cn.com/problems/word-search/
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.
回溯算法,直接看代码,很容易理解
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;
}
}
标签:visit ack 顺序 单词 exist || ble i++ site
原文地址:https://www.cnblogs.com/powercai/p/10960468.html