# leetcode #79 in cpp

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

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

• eddy_liu
• 2016年03月20日 15:07
• 1017

## LeetCode79——Word Search

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

## LeetCode 79（Word Search）java

• Jassy1994
• 2017年01月04日 22:54
• 624

## LeetCode - 79. Word Search

• shenzhu0127
• 2016年07月17日 16:19
• 848

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

• 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
• 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
• 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
• 2015年07月18日 17:12
• 260

举报原因： 您举报文章：leetcode #79 in cpp 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)