79. Word Search (Graph; DFS)

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 letter cell may not be used more than once. 所以在遍历时要为每个节点标记是否访问过

For example,
Given board =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

思路:对每个方向递归调用,带回溯的DFS

class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) {
        if(board.empty()) return false;
        vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
        bool result = false;
        for(int i = 0; i < board.size(); i++)
        {
            for(int j = 0; j < board[0].size(); j++)
            {
                 result = dfs(board,word,i,j,0,visited);
                 if(result) return true;
                 visited[i][j] = false; //回溯
            }
        }
        return false;
    }

    bool dfs(vector<vector<char> > &board, string word, int i, int j, int depth, vector<vector<bool>> &visited)
    {
        if(board[i][j] != word[depth]) return false;
        if(depth == word.length()-1) return true;
        visited[i][j] = true;
        if(j < board[0].size()-1 && !visited[i][j+1]){ //向右
            if(dfs(board,word, i, j+1, depth+1,visited)) return true;
            else visited[i][j+1] = false;  //回溯
        }
        if(j > 0 && !visited[i][j-1]) //向左
        {
            if(dfs(board,word, i, j-1, depth+1,visited)) return true;
            else visited[i][j-1] = false; //回溯
        }
        if(i < board.size()-1 && !visited[i+1][j]) //向下
        {
            if(dfs(board,word, i+1, j, depth+1,visited)) return true;
            else visited[i+1][j] = false; //回溯
        }
        if(i > 0  && !visited[i-1][j]) //向上
        {
            if(dfs(board, word, i-1, j, depth+1,visited)) return true;
            else visited[i-1][j] = false;  //回溯
        }
        return false;
    }
};
时间: 2024-10-07 01:40:35

79. Word Search (Graph; DFS)的相关文章

刷题79. Word Search

一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问题,就先看正确的写法,下面是回溯法的代码: class Solution { public: int m,n; //左 上 右 下 int dx[4] = {-1,0,1,0}; int dy[4] = {0,1,0,-1}; bool exist(vector<vector<char>&g

79. Word Search/212. Word Search II--图的back tracking -- tier tree 待续

79题, 给你一个二维的board, 只能往上下左右四个方向走,为你是否能找到单词. board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] Given word = "ABCCED", return true. Given word = "SEE", return true. Given word = "ABCB", return false. 分析: 算法并不难,

(Java) LeetCode 79. Word Search —— 单词搜索

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 letter cell may not be us

【Leetcode】79. Word Search

思路: 遍历矩阵,结合dfs解即可. #include <iostream> #include <vector> using namespace std; class Solution { public: Solution() {} bool exist(vector<vector<char>>& board, string word) { //初始化都没有被访问过 this->rowCount = board.size(); if (rowC

79. Word Search

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 letter cell may not be us

leetCode 79.Word Search (词搜索) 解题思路和方法

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board = [ ["ABCE"]

leetcode 79 Word Search ----- 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 or vertically neighboring. The same letter cell may not be us

79. Word Search java solutions

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 letter cell may not be us

LeetCode 79. Word Search单词搜索 (C++)

题目: 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 letter cell may not b