LeetCode: Word Search [079]

【题目】

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.

【题意】

给定一个二维字母矩阵board和单词word, 判断单词是否存在于board中,单词可以用board中的相邻字母连接而成,可以是上下相邻,也可以是左右相邻。同一个cell不能重复使用。

【思路】

先在board中找到word的第一个字符,然后用DFS判断

【代码】

class Solution {
public:
    vector<vector<bool> > isVisited;
    bool search(vector<vector<char> >&board, int i, int j, string&word, int k){
        // board[i][j]和word[k-1]吻合,以此为起点判断剩余的字符

        isVisited[i][j]=true;

        if(k==word.length())return true;

        //判断上方的位置
        if(i-1>=0 && !isVisited[i-1][j] && board[i-1][j]==word[k] && search(board, i-1, j, word, k+1))return true;
        //判断右面的位置
        if(j+1<board[0].size() && !isVisited[i][j+1] && board[i][j+1]==word[k] && search(board, i, j+1, word, k+1))return true;
        //判断下面的位置
        if(i+1<board.size() && !isVisited[i+1][j] && board[i+1][j]==word[k] && search(board, i+1, j, word, k+1))return true;
        //判断左面的位置
        if(j-1>=0 && !isVisited[i][j-1] && board[i][j-1]==word[k] && search(board, i, j-1, word, k+1))return true;

        isVisited[i][j]=false;

        return false;
    }

    bool exist(vector<vector<char> > &board, string word) {
        if(word.length()==0)return false;
        int rows = board.size();
        if(rows==0) return false;
        int cols = board[0].size();
        if(cols==0)return false;

        //定位首字符
        isVisited=vector<vector<bool> >(rows, vector<bool>(cols, false));
        for(int i=0; i<rows; i++){
            for(int j=0; j<cols; j++){
                if(board[i][j]==word[0]){
                    if(search(board, i, j, word, 1))return true;
                }
            }
        }
        return false;
    }
};

LeetCode: Word Search [079],布布扣,bubuko.com

时间: 2024-08-08 22:06:35

LeetCode: Word Search [079]的相关文章

[LeetCode] Word Search [37]

题目 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

[LeetCode] Word Search II

A simple combination of Implement Trie (Prefix Tree) and Word Search. If you've solved them, this problem will become easy :-) The following code is based on DFS and should be self-explanatory enough. Well, just go ahead and read it. It is long but c

LeetCode: Word Search 解题报告

Word SearchGiven 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 ma

[Leetcode] 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] Word Search II 词语搜索之二

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

[LeetCode] 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] Word Search

题目:(Backtrancing) 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

[LeetCode]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 --- 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 n