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 true;

        // 判断先决条件
        if(i<0 || j<0 || i>=n || j>=m || visit[i][j] || word[idx]!=board[i][j])
            return false;

        visit[i][j]=true;

        bool ret= dfs(i-1,j,board,idx+1,word) || dfs(i+1,j,board,idx+1,word) || dfs(i,j-1,board,idx+1,word) || dfs(i,j+1,board,idx+1,word);
        visit[i][j] = false;
        return ret;
    }

    bool exist(vector<vector<char>>& board, string word) {
        // 条件判断
        n= board.size();
        if(n==0)
            return false;
        m= board[0].size();
        if (m==0)
            return false;

        visit.resize(n,vector<bool>(m, false));

        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j)
            {
                if(dfs(i,j,board,0,word))
                {
                    return true;
                }
            }
        }
        return false;
    }
};

原文地址:https://www.cnblogs.com/randyniu/p/9229134.html

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

Leetcode 79. 单词搜索的相关文章

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 =

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

给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. 想法:本题跟我们9021 quiz7-8的类型是一样的,9024也用C写过一次,都是在二维数组里搜索,用回溯算法,今天脑袋有点不清醒,以后多刷几次. 学到的点: 1. 每一步递归中,都要注意判断 start_x,start_y的取值范围 2. 学到了python里面的语法还可以直接大于小于判断.

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 0079. Word Search单词搜索【Python】

LeetCode 0079. Word Search单词搜索[Medium][Python][DFS] Problem LeetCode 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 horiz

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

[华为机试真题]66.单词搜索

题目 代码 /*--------------------------------------- * 日期:2015-07-06 * 作者:SJF0115 * 题目:WordSearch * 来源:华为机试真题 -----------------------------------------*/ #include <iostream> #include <string> #include <vector> #include <stack> #include

【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