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 used more than once.

For example,
Given board =

[
  [‘A‘,‘B‘,‘C‘,‘E‘],
  [‘S‘,‘F‘,‘C‘,‘S‘],
  [‘A‘,‘D‘,‘E‘,‘E‘]
]

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

思路:DFS。对Board的每一个字母,如果其正好等于word[0],就以此作为help函数的入口,调用help函数。使用pos数组记录字符是否已经被使用。解释一下help函数各个参数的意义:help(vector<vector<char>> &board,vector<vector<bool>> &pos,string &word,int cur,int x,int y) ,pos记录字符是否被使用,如果pos[i][j]=true,表示board[i][j]字符已经被使用。word是最终要匹配的单词。cur表示当前已经处理了几个字符。x,y表示前一个字符的位置。如果cur等于字符串word的长度,表示已经找到了单词word,直接返回true。然后尝试从四个方向中的合法位置中去匹配字符word[cur],能够匹配就递归调用help(board,pos,word,cur+1,pos_x,pos_y)。如果递归函数返回true,表示找到了单词word,返回true。如果一直找不到word,就返回false。

  1. class Solution {
    private:
        int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    public:
        bool help(vector<vector<char>> &board,vector<vector<bool>> &pos,string &word,int cur,int x,int y){
            if(cur==word.length())
                return true;
            for(int i=0;i<4;i++){
                int pos_x=x+dir[i][0];
                int pos_y=y+dir[i][1];
                if(pos_x>=0&&pos_x<board.size()&&pos_y>=0&&pos_y<board[0].size()&&!pos[pos_x][pos_y]&&board[pos_x][pos_y]==word[cur]){
                    pos[pos_x][pos_y]=true;
                    if(help(board,pos,word,cur+1,pos_x,pos_y))
                        return true;
                    pos[pos_x][pos_y]=false;
                }
            }
            return false;
        }
        bool exist(vector<vector<char>>& board, string word) {
            if(word.length()==0)
                return true;
            int m=board.size();
            int n=board[0].size();
            vector<vector<bool>>pos(m,vector<bool>(n,false));
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    if(word[0]==board[i][j]){
                        pos[i][j]=true;
                        if(help(board,pos,word,1,i,j))
                            return true;
                        pos[i][j]=false;
                    }
                }
            }
            return false;
        }
    };
时间: 2024-10-13 20:10:18

79. Word Search的相关文章

刷题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. 分析: 算法并不难,

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

思路: 遍历矩阵,结合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

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 (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 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

(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单词搜索 (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