当前位置:网站首页>79. Word search [DFS + backtracking visit + traversal starting point]
79. Word search [DFS + backtracking visit + traversal starting point]
2022-07-01 12:36:00 【White speed Dragon King's review】

analysis
use dfs + visit + The idea of backtracking
Then find the right entrance
Take a look at the next position up and down, left and right. It's suitable to take one ( Here we use visit to flash back )
dfs Traverse all possible positions , As long as there is a penny dfs return True
So the whole thing is True Of , If there is no return True
unfortunately , That can only be False
Finally, if you can go to the last position, you can return
ac code
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
m, n = len(board), len(board[0])
k = len(word)
# dfs + to flash back
def dfs(r, c, idx):
if idx == k - 1:
return True
for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= nr < m and 0 <= nc < n and word[idx + 1] == board[nr][nc] and visit[nr][nc] is False:
visit[nr][nc] = True
temp = dfs(nr, nc, idx + 1)
if temp: return True
visit[nr][nc] = False
return False
# Traverse each starting point
for i in range(m):
for j in range(n):
visit = [[False] * n for _ in range(m)]
if board[i][j] == word[0]:
visit[i][j] = True
if dfs(i, j, 0): return True
return False
summary
dfs The parameters of record the current position and the current position of the last element that has been placed idx
The success condition is to reach the last index
边栏推荐
- 腾讯总考epoll, 很烦
- List of QT players [easy to understand]
- Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
- 数论基础及其代码实现
- 买卖其实也有风险
- How to use opcache, an optimization acceleration component of PHP
- Ansible相关内容梳理
- [Maui] add click events for label, image and other controls
- 用.Net Core接入微信公众号开发
- [20211129] jupyter notebook remote server configuration
猜你喜欢

79. 单词搜索【dfs + 回溯visit + 遍历起点】

基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能

被锡膏坑了一把

循环链表--

Onenet Internet of things platform - mqtts product equipment connected to the platform

Chapter 14 signals (IV) - examples of multi process tasks

VS Code 设置代码自动保存

Ipv6-6to4 experiment
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

leetcode:226. 翻转二叉树【dfs翻转】
随机推荐
Relationship between accuracy factor (DOP) and covariance in GPS data (reference link)
redis探索之缓存一致性
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
6.30模拟赛总结
Blue Bridge Cup multi interface switching processing (enumeration plus state machine method)
List of QT players [easy to understand]
STM32 project practice (1) introduction and use of photosensitive resistor
6.30 simulation summary
腾讯安全联合毕马威发布监管科技白皮书,解析“3+3”热点应用场景
Question d'entrevue de Huawei: recrutement
Ansible相关内容梳理
Wechat applet - 80 practical examples of wechat applet projects
使用BurpSuite对app抓包教程
Sort out relevant contents of ansible
Fatal error: execution: there is no such file or directory
[JS advanced] promise explanation
题目 1004: 母牛的故事(递推)
Mysql database knowledge collation
数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
【MAUI】为 Label、Image 等控件添加点击事件