-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethod_1.py
More file actions
66 lines (53 loc) · 1.78 KB
/
Copy pathmethod_1.py
File metadata and controls
66 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/9/13 00:24
# @Author : cancan
# @File : method_1.py
# @Function : 单词搜索
"""
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
给定 word = "ABCCED", 返回 true
给定 word = "SEE", 返回 true
给定 word = "ABCB", 返回 false
提示:
board 和 word 中只包含大写和小写英文字母。
1 <= board.length <= 200
1 <= board[i].length <= 200
1 <= word.length <= 10^3
"""
from typing import List
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
self.board = board
self.word = word
self.maxH = len(board)
self.maxW = len(board[0])
self.maxLen = len(word)
for i in range(self.maxH):
for j in range(self.maxW):
key = "%s-%s" % (i, j)
if self.dfs(0, i, j, {key: True}):
return True
return False
def dfs(self, idx, i, j, tmp):
if i < 0 or i >= self.maxH or j < 0 or j >= self.maxW or self.board[i][j] != self.word[idx]:
return False
idx += 1
if idx >= self.maxLen:
return True
for item in [[i, j + 1], [i, j - 1], [i + 1, j], [i - 1, j]]:
key = "%s-%s" % (item[0], item[1])
if key not in tmp:
tmp[key] = True
if self.dfs(idx, item[0], item[1], tmp):
return True
del tmp[key]
return False