**一. 題目描述**
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 neighbouring. 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.
**二. 題目分析**
題目的大意是,給定一個board字符矩陣和一個word字符串,可以從矩陣中任意一點開始經過上下左右的方式走,每個點只能走一次,若存在一條連續的路徑,路徑上的字符等于給定的word字符串的組合,則返回true,否則返回false。
解決的方法類似于走迷宮,使用遞歸回溯即可。由于走過的路徑需要排除,因此構造一個輔助數組記錄走過的位置,防止同一個位置被使用多次。
**三. 示例代碼**
~~~
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
bool exist(vector<vector<char> > &board, string word)
{
const int x = board.size();
const int y = board[0].size();
// 用于記錄走過的路徑
vector<vector<bool> > way(x, vector<bool>(y, false));
for (int i = 0; i < x; ++i)
{
for (int j = 0; j < y; ++j)
{
if (dfs(board, way, word, 0, i, j))
return true;
}
}
return false;
}
private:
bool dfs(vector<vector<char> > &board, vector<vector<bool> > way, string word, int index, int x, int y)
{
if (index == word.size()) // 單詞完成匹配
return true;
if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size()) // 超出邊界
return false;
if (word[index] != board[x][y]) // 遇到不匹配
return false;
if (way[x][y]) // 該格已經走過,返回false
return false;
// 若該格未曾走過,可進行遞歸
way[x][y] = true;
bool result = dfs(board, way, word, index + 1, x + 1, y) || // 往上掃描
dfs(board, way, word, index + 1, x - 1, y) || // 往下掃描
dfs(board, way, word, index + 1, x, y + 1) || // 往右掃描
dfs(board, way, word, index + 1, x, y - 1); // 往左掃描
way[x][y] = false;
return result;
}
};
~~~

- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching