Leetcode_word-search(c++ version)

地址:http://oj.leetcode.com/problems/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 =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

思路:dfs,维护好标记。刚开始新开辟了一个和board一样大的bool型的二维vector,遇到大case的时候会TLE。

解决方法是 直接对board进行标记就可以了。

对board进行标记和维护的过程类似回溯。

参考代码:

class Solution {
public:
    int row, col;
    bool dfs(vector<vector<char> >&map, int pos_row, int pos_col, string word, int left_len)
    {
        if(!left_len)
            return true;
        char dest_ch = word[word.length()-left_len];
        if(pos_row+1<this->row && map[pos_row+1][pos_col]==dest_ch)
        {
            map[pos_row+1][pos_col]='*';
            if(dfs(map, pos_row+1, pos_col, word, left_len-1))
                return true;
            map[pos_row+1][pos_col]=dest_ch;
        }
        if(pos_row-1>=0 && map[pos_row-1][pos_col]==dest_ch)
        {
            map[pos_row-1][pos_col]='*';
            if(dfs(map, pos_row-1, pos_col, word, left_len-1))
                return true;
            map[pos_row-1][pos_col]=dest_ch;
        }
        if(pos_col+1<this->col && map[pos_row][pos_col+1]==dest_ch)
        {
            map[pos_row][pos_col+1]='*';
            if(dfs(map, pos_row, pos_col+1, word, left_len-1))
                return true;
            map[pos_row][pos_col+1]=dest_ch;
        }
        if(pos_col-1>=0 && map[pos_row][pos_col-1]==dest_ch)
        {
            map[pos_row][pos_col-1]='*';
            if(dfs(map, pos_row, pos_col-1, word, left_len-1))
                return true;
            map[pos_row][pos_col-1]=dest_ch;
        }
        return false;
    }
    
    bool exist(vector<vector<char> > &board, string word) {
        if(board.empty() || word.empty())
            return false;
        this->row = board.size();
        this->col = board[0].size();
                
        for(int i = 0; i<this->row; ++i)
        {
            for(int j = 0; j<this->col; ++j)
            {
                if(board[i][j]==word[0])
                {
                    board[i][j]='*';
                    if(dfs(board, i, j, word, word.length()-1))
                        return true;
                    board[i][j]=word[0];
                }
            }
        }
        return false;
    }
};

//Second trial, more clean code

class Solution {
private:
    int row, col;
    int x[4] = {-1, 1, 0, 0}, y[4] = {0, 0, -1, 1};
    bool dfs(vector<vector<char> >&board, string word, int r, int l) {
        if(word.empty())
            return true;
        int nr, nl;
        for(int k = 0; k<4; ++k) {
            nr = r+x[k];
            nl = l+y[k];
            char ch;
            if(nr>=0 && nr<row && nl>=0 && nl<col && board[nr][nl] == word[0]) {
                ch = board[nr][nl];
                board[nr][nl] = '*';
                if(word.substr(1).empty())
                    return true;
                if(dfs(board, word.substr(1), nr, nl))
                    return true;
                board[nr][nl] = ch;
            }
        }
        return false;
    }
public:
    bool exist(vector<vector<char> > &board, string word) {
        if(board.empty() || word.empty())
            return false;
        row = board.size();
        col = board[0].size();
        char ch;
        for(int i = 0; i<row; ++i){
            for(int j = 0; j<col; ++j) {
                if(board[i][j]==word[0]){
                    ch = board[i][j];
                    board[i][j] = '*';
                    if(dfs(board, word.substr(1), i, j))
                        return true;
                    board[i][j] = ch;
                }
            }
        }
        return false;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值