{ "question_id": 79, "question_title": "单词搜索", "difficulty": "中等", "question_content": "

给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

 

示例 1:

\"\"
输入:board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCCED\"
输出:
true

示例 2:

\"\"
输入:board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"SEE\"
输出:
true

示例 3:

\"\"
输入:board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCB\"
输出:
false

 

提示:

 

进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

", "topic_link": "https://bbs.csdn.net/topics/600470927", "cpp": "#include \n#include \n#include \n#include \nstatic bool dfs(char *word, char **board, bool *used,\n\t\t\t\tint row, int col, int row_size, int col_size)\n{\n\tif (board[row][col] != *word)\n\t{\n\t\treturn false;\n\t}\n\tused[row * col_size + col] = true;\n\tif (*(word + 1) == '\\0')\n\t{\n\t\treturn true;\n\t}\n\tbool result = false;\n\tif (row > 0 && !used[(row - 1) * col_size + col])\n\t{\n\t\tresult = dfs(word + 1, board, used, row - 1, col, row_size, col_size);\n\t}\n\tif (!result && row < row_size - 1 && !used[(row + 1) * col_size + col])\n\t{\n\t\tresult = dfs(word + 1, board, used, row + 1, col, row_size, col_size);\n\t}\n\tif (!result && col > 0 && !used[row * col_size + col - 1])\n\t{\n\t\tresult = dfs(word + 1, board, used, row, col - 1, row_size, col_size);\n\t}\n\tif (!result && col < col_size - 1 && !used[row * col_size + col + 1])\n\t{\n\t\tresult = dfs(word + 1, board, used, row, col + 1, row_size, col_size);\n\t}\n\tused[row * col_size + col] = false;\n\treturn result;\n}\nstatic bool exist(char **board, int boardRowSize, int boardColSize, char *word)\n{\n\tint i, j;\n\tint len = strlen(word);\n\tif (len > boardRowSize * boardColSize)\n\t{\n\t\treturn false;\n\t}\n\tbool *used = malloc(boardRowSize * boardColSize);\n\tfor (i = 0; i < boardRowSize; i++)\n\t{\n\t\tfor (j = 0; j < boardColSize; j++)\n\t\t{\n\t\t\tmemset(used, false, boardRowSize * boardColSize);\n\t\t\tif (dfs(word, board, used, i, j, boardRowSize, boardColSize))\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test word row1 row2...\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? \"true\" : \"false\");\n\treturn 0;\n}", "java": "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", "js": "", "python": "class Solution(object):\n\tdef exist(self, board, word):\n\t\t\"\"\"\n\t\t:type board: List[List[str]]\n\t\t:type word: str\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tcheck_board = [[True] * len(board[0]) for _ in range(len(board))]\n\t\tfor i in range(len(board)):\n\t\t\tfor j in range(len(board[0])):\n\t\t\t\tif board[i][j] == word[0] and check_board:\n\t\t\t\t\tcheck_board[i][j] = False\n\t\t\t\t\tres = self.check_exist(check_board, board, word, 1, len(word), i, j)\n\t\t\t\t\tif res:\n\t\t\t\t\t\treturn True\n\t\t\t\t\tcheck_board[i][j] = True\n\t\treturn False\n\tdef check_exist(self, check_board, board, word, index, ls, row, col):\n\t\tif index == ls:\n\t\t\treturn True\n\t\tfor temp in [(0, 1),(0, -1),(1, 0),(-1, 0)]:\n\t\t\tcurr_row = row + temp[0]\n\t\t\tcurr_col = col + temp[1]\n\t\t\tif curr_row >= 0 and curr_row < len(board) and curr_col >= 0 and curr_col < len(board[0]):\n\t\t\t\tif check_board[curr_row][curr_col] and board[curr_row][curr_col] == word[index]:\n\t\t\t\t\tcheck_board[curr_row][curr_col] = False\n\t\t\t\t\tres = self.check_exist(check_board, board, word, index + 1, len(word), curr_row, curr_col)\n\t\t\t\t\tif res:\n\t\t\t\t\t\treturn res\n\t\t\t\t\tcheck_board[curr_row][curr_col] = True\n\t\treturn False\nif __name__ == \"__main__\":\n\ts = Solution()\n\tprint (s.exist(board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCCED\"))", "status": 1, "keywords": "数组,回溯,矩阵", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://github.com/zhangyu345293721/leetcode" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/78/78_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/78/78_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/78/78_java.ipynb?type=file" }, "notebook_enable": 1 }