leetcode Word Search

原创 2015年07月07日 17:45:32

题目

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

分析

回溯法。

代码

class Solution {
public:
    bool search(vector<vector<char>>& board, vector<vector<bool>> &flag, int x, int y, string &word, int start){
        int m = board.size();
        int n = board[0].size();
        if(start == word.length())
            return true;
        if(x < 0 || x >= m || y < 0 || y >= n || flag[x][y] || word.at(start) != board[x][y])
            return false;
        flag[x][y] = true;
        bool ans = search(board, flag, x+1, y, word, start+1) ||
                    search(board, flag, x-1, y, word, start+1) ||
                    search(board, flag, x, y+1, word, start+1) ||
                    search(board, flag, x, y-1, word, start+1);
        flag[x][y] = false;
        return ans;
    }
    bool exist(vector<vector<char>>& board, string word) {
        int m = board.size();
        if(m == 0)
            return false;
        int n = board[0].size();
        vector<vector<bool> > flag(m, vector<bool> (n, false));
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                    if(search(board, flag, i, j, word, 0))
                        return true;
            }
        }
        return false;
    }
};

相关文章推荐

Leetcode 79. Word Search (Medium) (cpp)

Leetcode 79. Word Search (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年07月31日 22:26
  • 164

[LeetCode212] Word Search II

HARDGiven a 2D board and a list of words from the dictionary, find all words in the board.Each word ...

LeetCode 79. Word Search

递归搜索即可,注意标记已访问过的点。 代码: class Solution { public: bool exist(vector > &board, string word) {...

[LeetCode] Word Search 的非递归实现

leetcode word search 的非递归实现
  • hjh00
  • hjh00
  • 2015年11月01日 23:39
  • 445

LeetCode | Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro...

LeetCode 079 Word Search

代码Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from ...

Leetcode 79 - Word Search(dfs)

题意求一个字符串是否能从矩形内某一点出发,沿上下左右走得到。思路dfs。算法1用use[i][j]记录位置[i, j]是否被访问过,dfs就好。算法2我们访问一个位置的时候,把它记为’#’,访问完还原...
  • Lzedo
  • Lzedo
  • 2017年02月17日 22:27
  • 115

212 Word Search II [Leetcode]

题目内容: Given a 2D board and a list of words from the dictionary, find all words in the board. ...

[leetCode]79(Word Search)

题目如下: Given a 2D board and a word, find if the word exists in the grid. The word can be cons...
  • isssen
  • isssen
  • 2015年04月21日 20:47
  • 210

[leetcode]Word Search

问题描述: Given a 2D board and a word, find if the word exists in the grid. The word can be construc...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Word Search
举报原因:
原因补充:

(最多只允许输入30个字)