当前位置:网站首页>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
边栏推荐
- [20220605] Literature Translation -- visualization in virtual reality: a systematic review
- 天青色等烟雨
- JS related interview questions and answers (1)
- Good luck brought by years of persistence
- Four years after graduation: work, resign, get married, buy a house
- 基因检测,如何帮助患者对抗疾病?
- 華為面試題: 招聘
- Friends day 2022
- ROS2 Foxy depthai_ros教程
- 用.Net Core接入微信公众号开发
猜你喜欢
![[Suanli network] technological innovation of Suanli Network -- key technology of operation service](/img/80/6e3648c88d309516d4bc29db9c153c.jpg)
[Suanli network] technological innovation of Suanli Network -- key technology of operation service

redis探索之缓存击穿、缓存雪崩、缓存穿透

【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾

被锡膏坑了一把

leetcode:226. 翻转二叉树【dfs翻转】

本科毕业四年:工作,辞职,结婚,买房

VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
![[JS] interview questions](/img/f3/8cf430b999980190a250f89537715e.jpg)
[JS] interview questions

栈-------
![[speech signal processing] 3 speech signal visualization -- prosody](/img/06/5f57f9dfe3a0f2f70022706f7d4d17.jpg)
[speech signal processing] 3 speech signal visualization -- prosody
随机推荐
Queue operation---
79. 单词搜索【dfs + 回溯visit + 遍历起点】
[some notes]
[20211129] jupyter notebook remote server configuration
二叉树的链式存储
Blocking sockets的读写操作该怎么玩?
数论基础及其代码实现
Question d'entrevue de Huawei: recrutement
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
MySQL common functions
本科毕业四年:工作,辞职,结婚,买房
How to install php7 and perform performance test using yum
使用nvm管理nodejs(把高版本降级为低版本)
被锡膏坑了一把
Wechat simulated geographical location_ Camouflage wechat location
【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
Onenet Internet of things platform - mqtts product equipment connected to the platform