|
| 1 | +/* |
| 2 | + * @lc app=leetcode.cn id=79 lang=cpp |
| 3 | + * |
| 4 | + * [79] 单词搜索 |
| 5 | + * |
| 6 | + * https://leetcode.cn/problems/word-search/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (46.29%) |
| 10 | + * Likes: 1601 |
| 11 | + * Dislikes: 0 |
| 12 | + * Total Accepted: 428.5K |
| 13 | + * Total Submissions: 925.7K |
| 14 | + * Testcase Example: |
| 15 | + * '[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]\n"ABCCED"' |
| 16 | + * |
| 17 | + * 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word |
| 18 | + * 存在于网格中,返回 true ;否则,返回 false 。 |
| 19 | + * |
| 20 | + * 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 |
| 21 | + * |
| 22 | + * |
| 23 | + * |
| 24 | + * 示例 1: |
| 25 | + * |
| 26 | + * |
| 27 | + * 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = |
| 28 | + * "ABCCED" |
| 29 | + * 输出:true |
| 30 | + * |
| 31 | + * |
| 32 | + * 示例 2: |
| 33 | + * |
| 34 | + * |
| 35 | + * 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = |
| 36 | + * "SEE" |
| 37 | + * 输出:true |
| 38 | + * |
| 39 | + * |
| 40 | + * 示例 3: |
| 41 | + * |
| 42 | + * |
| 43 | + * 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = |
| 44 | + * "ABCB" |
| 45 | + * 输出:false |
| 46 | + * |
| 47 | + * |
| 48 | + * |
| 49 | + * |
| 50 | + * 提示: |
| 51 | + * |
| 52 | + * |
| 53 | + * m == board.length |
| 54 | + * n = board[i].length |
| 55 | + * 1 |
| 56 | + * 1 |
| 57 | + * board 和 word 仅由大小写英文字母组成 |
| 58 | + * |
| 59 | + * |
| 60 | + * |
| 61 | + * |
| 62 | + * 进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board |
| 63 | + * 更大的情况下可以更快解决问题? |
| 64 | + * |
| 65 | + */ |
| 66 | + |
| 67 | +#include <string> |
| 68 | +#include <vector> |
| 69 | +using namespace std; |
| 70 | + |
| 71 | +// @lc code=start |
| 72 | +class Solution { |
| 73 | +public: |
| 74 | + // dfs+访问标记,标记矩阵可修改输入数组 |
| 75 | + bool exist(vector<vector<char>> &board, string word) { |
| 76 | + if (board.empty()) { |
| 77 | + return word.empty(); |
| 78 | + } |
| 79 | + |
| 80 | + for (int i = 0; i < board.size(); i++) { |
| 81 | + for (int j = 0; j < board[0].size(); j++) { |
| 82 | + if (dfs(board, i, j, word, 0)) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | +private: |
| 91 | + bool dfs(vector<vector<char>> &board, int i, int j, string word, int idx) { |
| 92 | + if (idx >= word.size()) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size()) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + if (board[i][j] != word[idx]) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + char ch = board[i][j]; |
| 102 | + board[i][j] = '-'; |
| 103 | + bool ret = dfs(board, i - 1, j, word, idx + 1) || |
| 104 | + dfs(board, i + 1, j, word, idx + 1) || |
| 105 | + dfs(board, i, j - 1, word, idx + 1) || |
| 106 | + dfs(board, i, j + 1, word, idx + 1); |
| 107 | + board[i][j] = ch; // 回溯 |
| 108 | + return ret; |
| 109 | + } |
| 110 | +}; |
| 111 | +// @lc code=end |
0 commit comments