{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "0080108f11a948a9b13c5fd11952b680",
  "keywords": "数组,回溯,矩阵",
  "title": "单词搜索",
  "desc": [
    {
      "content": "\n<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p><p>单词必须按照字母顺序，通过相邻的单元格内的字母构成，其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word2.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入：</strong>board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCCED\"<strong><br />输出：</strong>true</pre><p><strong>示例 2：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word-1.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入：</strong>board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"SEE\"<strong><br />输出：</strong>true</pre><p><strong>示例 3：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word3.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入：</strong>board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCB\"<strong><br />输出：</strong>false</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>m == board.length</code></li>\t<li><code>n = board[i].length</code></li>\t<li><code>1 <= m, n <= 6</code></li>\t<li><code>1 <= word.length <= 15</code></li>\t<li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li></ul><p> </p><p><strong>进阶：</strong>你可以使用搜索剪枝的技术来优化解决方案，使其在 <code>board</code> 更大的情况下可以更快解决问题？</p>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic boolean exist(char[][] board, String word) {\n\t\tint cl = board.length;\n\t\tint rl = board[0].length;\n\t\tboolean[][] flag = new boolean[cl][rl];\n\t\tfor (int i = 0; i < cl; i++) {\n\t\t\tfor (int j = 0; j < rl; j++) {\n\t\t\t\tif (find(board, word, flag, i, j, 0))\n\t\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tpublic boolean find(char[][] board, String word, boolean[][] flag, int i, int j, int index) {\n\t\tint cl = board.length;\n\t\tint rl = board[0].length;\n\t\tif (word.length() == index)\n\t\t\treturn true;\n\t\tif (i < 0 || i >= cl || j >= rl || j < 0)\n\t\t\treturn false;\n\t\tif (flag[i][j] || word.charAt(index) != board[i][j])\n\t\t\treturn false;\n\t\tflag[i][j] = true;\n\t\tboolean judge = find(board, word, flag, i - 1, j, index + 1) || find(board, word, flag, i + 1, j, index + 1)\n\t\t\t\t|| find(board, word, flag, i, j - 1, index + 1) || find(board, word, flag, i, j + 1, index + 1);\n\t\tflag[i][j] = false;\n\t\treturn judge;\n\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-d433cef163fe4e2fbdf5bef441506da8",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470927"
}