链接:
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.
我的解法: 写的很丑。。dfs函数的作用是判断给定的board和剩下的word,能不能在board位置[i,j]的四周找到word[0],如果找到就递归。给每次遍历过的地方赋值为0,避免重复走;给每次走的地方保存出来,为了不影响别的可能的时候的走法。
class Solution(object):
def exist(self, board, word):
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == word[0]:
w = board[i][j]
board[i][j] = 0
res = word[1:]
if self.dfs(board, res, i, j):
return True
board[i][j] = w
return False
def dfs(self, board, word, i, j):
if word == "":
return True
if i > 0:
if board[i-1][j]==word[0]:
w = board[i-1][j]
board[i-1][j] = 0
res = word[1:]
if self.dfs(board, res, i-1, j):
return True
board[i-1][j] = w
if i < len(board)-1:
if board[i+1][j]==word[0]:
w = board[i+1][j]
board[i+1][j] = 0
res = word[1:]
if self.dfs(board, res, i+1, j):
return True
board[i+1][j] = w
if j > 0:
if board[i][j-1]==word[0]:
w = board[i][j-1]
board[i][j-1] = 0
res = word[1:]
if self.dfs(board, res, i, j-1):
return True
board[i][j-1] = w
if j < len(board[0])-1:
if board[i][j+1]==word[0]:
w = board[i][j+1]
board[i][j+1] = 0
res = word[1:]
if self.dfs(board, res, i, j+1):
return True
board[i][j+1] = w
别人的解法: 思路类似,实现的好多了,所有情况都进dfs,returnFalse的条件增多。
def exist(self, board, word):
if not board:
return False
for i in xrange(len(board)):
for j in xrange(len(board[0])):
if self.dfs(board, i, j, word):
return True
return False
# check whether can find word, start at (i,j) position
def dfs(self, board, i, j, word):
if len(word) == 0: # all the characters are checked
return True
# 一定先判断i、j的边界问题,再判断board[i][j]的大小,不然board[i][j]会报错
if i<0 or i>=len(board) or j<0 or j>=len(board[0]) or word[0]!=board[i][j]:
return False
tmp = board[i][j] # first character is found, check the remaining part
board[i][j] = "#" # avoid visit agian
# check whether can find "word" along one direction
res = self.dfs(board, i+1, j, word[1:]) or self.dfs(board, i-1, j, word[1:]) \
or self.dfs(board, i, j+1, word[1:]) or self.dfs(board, i, j-1, word[1:])
board[i][j] = tmp
return res
别人的解法2: 思路类似,实现的好多了,将判断word[0] == board[i][j]放到子函数。
def exist(self, board, word):
if not word:
return True
if not board:
return False
for i in range(len(board)):
for j in range(len(board[0])):
if self.exist_helper(board, word, i, j):
return True
return False
def exist_helper(self, board, word, i, j):
if board[i][j] == word[0]:
if not word[1:]:
return True
board[i][j] = " " # indicate used cell
# check all adjacent cells
if i > 0 and self.exist_helper(board, word[1:], i-1, j):
return True
if i < len(board)-1 and self.exist_helper(board, word[1:], i+1, j):
return True
if j > 0 and self.exist_helper(board, word[1:], i, j-1):
return True
if j < len(board[0])-1 and self.exist_helper(board, word[1:], i, j+1):
return True
board[i][j] = word[0] # update the cell to its original value
return False
else:
return False