# word search

leetcode 第79题，图上的深搜题。

class Solution:
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
row = len(board)
col = len(board[0])

def dfs(current_x, current_y, board, index, word):

if index == len(word)-1:
return True
tmp = board[current_x][current_y]
board[current_x][current_y] = '#'
if current_x+1 < row and board[current_x+1][current_y] == word[index+1]:
if dfs(current_x+1, current_y, board, index+1, word):
return True
if current_x-1 >= 0 and board[current_x-1][current_y] == word[index+1]:
if dfs(current_x-1, current_y, board, index+1, word):
return True
if current_y+1 < col and board[current_x][current_y+1] == word[index+1]:
if dfs(current_x, current_y+1, board, index+1, word):
return True
if current_y-1 >= 0 and board[current_x][current_y-1] == word[index+1]:
if dfs(current_x, current_y-1, board, index+1, word):
return True
board[current_x][current_y] = tmp

return False

for i in range(row):
for j in range(col):
if board[i][j] == word[0]:
if dfs(i, j, board, 0, word):
return True
return False


07-25 712

05-20 7445
01-24 1071
05-21 1086
04-15 1805
06-30 708
07-17 1821
12-10 2108
12-08 2101
02-01 5755
12-02 725
04-23 59
06-01 1611
01-04 2173
09-23 1105
12-21 1067