leetcode #79 in cpp

原创 2016年06月02日 09:50:56

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.

Solution;

This is a basic DFS search for string. Simply find the first character and perform DFS in 4 direction for next characters. 

Code:

class Solution {
public:
    int dir[4][2] = {{1,0},{0,1}, {-1, 0}, {0,-1}}; 
    
    bool exist(vector<vector<char>>& board, string word) {
        if(board.empty()) return false;
        int m = board.size();
        int n = board[0].size();
        vector<vector<bool>> visited(m, vector<bool>(n, false)); 
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(board[i][j] == word[0]){//find the match of the first character, then start from this point to locate the string
                    visited[i][j] = true;//note this position as visited. This is because 1 letter could be used once. 
                    if(search(i, j, 1,visited, word, board))
                        return true;
                    else visited[i][j] = false;//if next characters not found, go to find another starting point
                }
            }
        }
        return false; 
    }
    
    bool search(int cur_row, int cur_col,int search_pos, vector<vector<bool>> &visited, string &word, vector<vector<char>> &board)
    {   
        if(search_pos >= word.length()) return true;//if searching position is over the string length, 
<span style="white-space:pre">						</span>//it means we successfully reach the end
        //search for the letter at search_ind
        for(int i = 0; i < 4; i ++){//search for 4 directions
            int row = cur_row + dir[i][0];
            int col = cur_col + dir[i][1];
<span style="white-space:pre">		</span>//see if this position is out of bound and if this position has the character we want to find. If character matches,
<span style="white-space:pre">		</span>// go to the next recurrence for next character
            if(row < board.size() && row >=0 && col < board[0].size() && col >= 0 && !visited[row][col] && board[row][col] == word[search_pos]){
                visited[row][col] = true;
                if(search(row, col, search_pos+1, visited, word, board))
                    return true;
                else
                    visited[row][col] = false;
            }
        }
        return false;
    }
};


LeetCode79:Word Search

Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from le...
  • u012501459
  • u012501459
  • 2015年07月02日 15:42
  • 923

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 ...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016年03月09日 12:45
  • 1489

LeetCode-79-Word Search(回溯法)-Medium

题意理解: 求解给定字符串str,是否在字符矩阵array中。要求str中相邻的两个字符,在array中也是相邻字符,并且要求,array中的同一字符,不能使用两次; 题目分析: 1. ...
  • eddy_liu
  • eddy_liu
  • 2016年03月20日 15:07
  • 1017

LeetCode79——Word Search

LeetCode79——Word Search 题意: 在一个字母板(二维数组)上找到连续的一跳折线(或者直线),线上字母连起来可以可以凑成我们的目标单词。 思路: DFS应该没什么问题了。 往上...
  • zhangxiao93
  • zhangxiao93
  • 2015年11月17日 21:53
  • 1921

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 ...
  • Jassy1994
  • Jassy1994
  • 2017年01月04日 22:54
  • 624

LeetCode - 79. Word Search

这道题目的总体思路算是比较清晰,使用双重循环迭代给出的matrix的每一个元素,在遇到每个元素的时候检测从当前这个元素向四个方向出发的单词是不是存在。根据上面的思路我们将检测从当前这个元素出发四个方向...
  • shenzhu0127
  • shenzhu0127
  • 2016年07月17日 16:19
  • 848

LeetCode Solution(持续更新,java>c++)

Title Add Date AC Rates
  • disappearedgod
  • disappearedgod
  • 2014年04月13日 21:52
  • 8833

[leetcode] 【分治法】 50. Pow(x, n)

Implement pow(x, n). 题意 实现幂函数。 题解 使用分治法求解。 分:将n分成n/2  直到n=0时,返回1; 治:对n为偶数,返回两数相乘的结果,奇数再乘多一个x...
  • u014654002
  • u014654002
  • 2016年06月21日 00:37
  • 381

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 l...
  • happyxuma1991
  • happyxuma1991
  • 2016年04月19日 14:54
  • 94

leetcode 79:LWord Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro...
  • le119126
  • le119126
  • 2015年07月18日 17:12
  • 260
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #79 in cpp
举报原因:
原因补充:

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