79. 单词搜索-回溯算法(leetcode)

给定一个二维网格和一个单词,找出该单词是否存在于网格中。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

想法:本题跟我们9021 quiz7-8的类型是一样的,9024也用C写过一次,都是在二维数组里搜索,用回溯算法,今天脑袋有点不清醒,以后多刷几次。

学到的点:

  1. 每一步递归中,都要注意判断 start_x,start_y的取值范围

  2. 学到了python里面的语法还可以直接大于小于判断。

    if 0<=start_x<noOfRow and 0<=start_y<noOfCol

class Solution:

def exist(self, board: List[List[str]], word: str) -> bool:

noOfRow = len(board)

noOfCol = len(board[0])

visited = [[False for _ in range(noOfCol)] for _ in range(noOfRow)]

for i in range(noOfRow):

for j in range(noOfCol):

if self.helper(i,j,board,visited,noOfRow,noOfCol,0,word):

print(i,j)

return True

return False

def helper(self,start_x,start_y,board,visited,noOfRow,noOfCol,point,word):

directions = [(0,1),(1,0),(0,-1),(-1,0)]

if point == len(word)-1:

if 0<=start_x<noOfRow and 0<=start_y<noOfCol and board[start_x][start_y] == word[point] and not visited[start_x][start_y]:

return True

else:

return False

if 0<=start_x<noOfRow \

and 0<=start_y<noOfCol and board[start_x][start_y] == word[point] and point<len(word) and not visited[start_x][start_y]:

visited[start_x][start_y] = True

for direction in directions:

new_x = start_x + direction[0]

new_y = start_y + direction[1]

if self.helper(new_x,new_y,board,visited,noOfRow,noOfCol,point+1,word):

return True

visited[start_x][start_y] = False

return False

原文地址:https://www.cnblogs.com/ChevisZhang/p/12442026.html

时间: 2024-12-19 11:58:05

79. 单词搜索-回溯算法(leetcode)的相关文章

131. 分割回文串-回溯算法 (leetcode)

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 代码: class Solution: def __init__(self): self.res = [] def partition(self, s: str) -> List[List[str]]: self.helper(s,[]) return self.res def helper(self,part_of_s,answerList): if not part_of_s: self.re

LeetCode——79. 单词搜索

给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. 示例: board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 tru

leetcode 79. 单词搜索(Word Search)

目录 题目描述: 示例: 解法: 题目描述: 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. 示例: board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word =

Leetcode 79. 单词搜索

#include <vector> class Solution { public: int n,m; //标记是否被访问过 vector<vector<bool>> visit; // idx(不包括)之前的都已经被访问过了 bool dfs(int i,int j,vector<vector<char>>& board, int idx, string word) { if(idx == word.size()) return tru

20191030-带返回值的回溯算法Leetcode解数独

题目描述 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次. 空白格用 '.' 表示. 一个数独. 答案被标成红色. Note: 给定的数独序列只包含数字 1-9 和字符 '.' . 你可以假设给定的数独只有唯一解. 给定数独永远是 9x9 形式的. 输入格式: [["5","3",".&

79. 单词搜索

class Solution { int n; int m; int dx[] = new int[] { -1, 0, 1, 0 }; int dy[] = new int[] { 0, -1, 0, 1 }; public boolean exist(char[][] board, String word) { if(board.length==0||board[0].length==0)return false; n = board.length; m = board[0].length;

LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same

【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】

[079-Word Search(单词搜索)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally

leetcode 46 Permutations Python 实现(回溯算法)

Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] 回溯算法自己的一个思路如下:需要保留数字使用情况,my_nums增加和删除元素等操作 1 class Solution: 2 def permute(self, nums: Li