当前位置:网站首页>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
边栏推荐
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
- Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
- 买卖其实也有风险
- Like the three foot platform
- 二叉树的链式存储
- Mobile note application
- 【脑洞大开】《西潮》及《走向世界丛书》
- 队列操作---
- Using burpsuite to capture app packages
- 微信模拟地理位置_伪装微信地理位置
猜你喜欢

Four years after graduation: work, resign, get married, buy a house

2022-06-28-06-29

IOS interview

用.Net Core接入微信公众号开发

Ipv6-6to4 experiment

codeforces -- 4B. Before an Exam

VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
![[20211129] jupyter notebook remote server configuration](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] jupyter notebook remote server configuration

leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】

Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
随机推荐
Circular linked list--
Double linked list related operations
【20211129】Jupyter Notebook遠程服務器配置
Ipv6-6to4 experiment
Stack-------
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
ANSI/UL 94 VTM薄质材料垂直燃烧测试
微信小程序 – 80个实用的微信小程序项目实例
Eurake分区理解
基因检测,如何帮助患者对抗疾病?
Exploration and practice of inress in kubernetes
BIM and safety in road maintenance-buildSmart Spain
腾讯总考epoll, 很烦
Mobile note application
二叉树的链式存储
STM32 project practice (1) introduction and use of photosensitive resistor
VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
ustime写出了bug
localtime居然不可重入,踩坑了
One year anniversary of bitbear live studio, hero rally order! I invite you to take a group photo!