码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Word Search

时间:2015-03-17 12:18:53      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

 

Hide Tags
 Array Backtracking
 
 
思路一:dfs,但是 Time Limit Exceeded
class Solution {
        vector<vector<bool> > canUse;
    public:
        bool dfs(int dep, int x, int y, vector<vector<char> >& board, string& word)
        {
            if(dep == word.size())
                return true;

            int row = board.size();
            int col = board[0].size();
            if(x < 0 || x >= row ||
                y <0 || y >= col )
                return false;

            if(board[x][y] != word[dep])
                return false;

            //mark
            canUse[x][y] = false;


            if(x < row-1 && canUse[x+1][y] )
            {
                if(dfs(dep+1, x+1, y, board, word))
                {
                    //mark
                    canUse[x][y] = true;
                    return true;
                }
            }

            if(x >0 && canUse[x-1][y] )
            {
                if(dfs(dep+1, x-1, y, board, word))
                {
                    //mark
                    canUse[x][y] = true;
                    return true;
                }
            }

            if(y < col-1 && canUse[x][y+1])
            {
                if(dfs(dep+1, x, y+1, board, word))
                {
                    //mark
                    canUse[x][y] = true;
                    return true;
                }
            }
            if(y >0 && canUse[x][y-1])
            {
                if(dfs(dep+1, x, y-1, board, word))
                {
                    //mark
                    canUse[x][y] = true;
                    return true;
                }
            }

            canUse[x][y] = true;

            return false;

        }

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

            canUse.clear();
            vector<bool> tmp(col, true);
            canUse.resize(row, tmp);

            for(int i = 0; i< row; i++)
            {
                for(int j = 0; j< col; j++)
                {
                    if(dfs(0, i, j, board, word ))
                        return true;
                }
            }
            return false;
        }
};

 

 

[LeetCode] Word Search

标签:

原文地址:http://www.cnblogs.com/diegodu/p/4343924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!