{ "type": "code_options", "author": "https://github.com/begeekmyfriend/leetcode", "source": "solution.md", "exercise_id": "485511cc5a7a4c6e8fdb39cd60d07047", "keywords": "数组,回溯,矩阵", "title": "单词搜索", "desc": [ { "content": "\n

给定一个 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 更大的情况下可以更快解决问题?

", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "#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}", "language": "cpp" }, "node_id": "dailycode-b3f62c12a2624288bb79b4210b44e046", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600470927" }