- class Solution:
- def exist(self, board: List[List[str]], word: str) -> bool:
- rows,cols=len(board),len(board[0])
- direct=[(0,1),(0,-1),(1,0),(-1,0)]
- visted=[[False]*cols for _ in range(rows)]
- def f(l,r,index):
- if(len(word)==index):
- return True
- if l<0 or l>=rows or r<0 or r>=cols or visted[l][r] or board[l][r]!=word[index]:
- return False
- visted[l][r]=True
- for dl,dr in direct:
- if f(l+dl,r+dr,index+1):
- return True
- visted[l][r]=False
- return False
- for i in range(rows):
- for j in range(cols):
- if f(i,j,0):
- return True
- return False
披着回溯皮的深搜