{"id":746,"date":"2020-09-26T11:17:59","date_gmt":"2020-09-26T03:17:59","guid":{"rendered":"http:\/\/39.96.58.60\/?p=746"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-79-%e5%8d%95%e8%af%8d%e6%90%9c%e7%b4%a2","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=746","title":{"rendered":"Leetcode 79. \u5355\u8bcd\u641c\u7d22"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u548c\u4e00\u4e2a\u5355\u8bcd\uff0c\u627e\u51fa\u8be5\u5355\u8bcd\u662f\u5426\u5b58\u5728\u4e8e\u7f51\u683c\u4e2d\u3002<\/p>\n<p>\u5355\u8bcd\u5fc5\u987b\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\uff0c\u901a\u8fc7\u76f8\u90bb\u7684\u5355\u5143\u683c\u5185\u7684\u5b57\u6bcd\u6784\u6210\uff0c\u5176\u4e2d\u201c\u76f8\u90bb\u201d\u5355\u5143\u683c\u662f\u90a3\u4e9b\u6c34\u5e73\u76f8\u90bb\u6216\u5782\u76f4\u76f8\u90bb\u7684\u5355\u5143\u683c\u3002\u540c\u4e00\u4e2a\u5355\u5143\u683c\u5185\u7684\u5b57\u6bcd\u4e0d\u5141\u8bb8\u88ab\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre>board =\n\n[\n\n  ['A','B','C','E'],\n\n  ['S','F','C','S'],\n\n  ['A','D','E','E']\n\n]\n\n\n\n\u7ed9\u5b9a word = \"<strong>ABCCED<\/strong>\", \u8fd4\u56de <strong>true<\/strong>\n\n\u7ed9\u5b9a word = \"<strong>SEE<\/strong>\", \u8fd4\u56de <strong>true<\/strong>\n\n\u7ed9\u5b9a word = \"<strong>ABCB<\/strong>\", \u8fd4\u56de <strong>false<\/strong><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>board<\/code> \u548c <code>word<\/code> \u4e2d\u53ea\u5305\u542b\u5927\u5199\u548c\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002<\/li>\n<li><code>1 &lt;= board.length &lt;= 200<\/code><\/li>\n<li><code>1 &lt;= board[i].length &lt;= 200<\/code><\/li>\n<li><code>1 &lt;= word.length &lt;= 10^3<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u7ec4\u3001 \u56de\u6eaf\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a352 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8615.26% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8638.04% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u4f9d\u6b21\u4ee5board\u4e0a\u6bcf\u4e2a\u70b9\u4f5c\u4e3a\u8d77\u59cb\u70b9\uff0c\u4e0eword\u4e2d\u5b57\u6bcd\u8fdb\u884c\u5339\u914d\n    \u82e5\u5339\u914d\uff0c\u5219\u5bfb\u627eboard\u5f53\u524d\u70b9\u56db\u5468\uff0c\u662f\u5426\u4e0eword\u4e2d\u4e0b\u4e00\u4e2a\u76f8\u5339\u914d\u7684\uff0c\u82e5\u627e\u4e0d\u5230\uff0c\u5219\u56de\u6eaf\n    \u5177\u4f53\u4e8b\u9879\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def exist(self, board: List[List[str]], word: str) -&gt; bool:\n        h, w, n = len(board), len(board[0]), len(word)  # \u7f51\u683c\u5bbd\u9ad8\uff0c\u4e0eword\u957f\u5ea6\n        if h*w &lt; n:\n            return False\n        directions = [(-1, 0), (0, -1), (0, 1), (1, 0)] # \u56db\u5468\u5750\u6807\u5411\u91cf\n        def backtrack(i, j, k, current):    #  \u7f51\u683c\u7b2ci\u884cj\u5217\uff0cword\u7b2ck\u4e2a\u5b57\u7b26\uff0c\u5f53\u524d\u5df2\u5339\u914d\u7684\u7f51\u683c\n            if len(current) == n:   # \u82e5\u5f53\u524d\u5339\u914d\u4ee5\u5339\u914d\u6240\u6709\u5b57\u7b26\uff0c\u8fd4\u56detrue\n                return True\n            if not (-1 &lt; i &lt; h and -1 &lt; j &lt; w): # \u7f51\u683c\u7d22\u5f15\u9650\u5236\n                return False\n            if board[i][j] == word[k]:  # \u5f53\u524d\u7f51\u683c\u5b57\u7b26\u4e0eword[k]\u5339\u914d\n                current.append((i, j))  # \u5c06\u5f53\u524d\u7f51\u683c\u5750\u6807\u6dfb\u52a0\u5230\u5df2\u5339\u914d\u961f\u5217\u4e2d\n                for direction in directions:    # \u5411\u56db\u5468\u5ef6\u4f38\n                    new_i, new_j = i+direction[0], j+direction[1]\n                    if (new_i, new_j) not in current :\n                        if backtrack(new_i, new_j, k+1, current):   # \u5bf9\u5f53\u524d\u7f51\u683c\u56db\u5468\u5339\u914d\u4e0b\u4e00\u4e2aword\u4e2d\u5b57\u7b26\n                            return True\n                current.pop()   # \u82e5\u56db\u5468\u4e0d\u5339\u914d\uff0c\u5219\u56de\u6eaf\n\n        for i in range(h):\n            for j in range(w):\n                if backtrack(i, j, 0, []):\n                    return True\n        return False\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a304 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8625.04% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8675.00% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u66f4\u6539\u4e86\u90e8\u5206\u903b\u8f91\n    \u73b0\u5728\u4ece\u4e00\u4e2a\u5df2\u7ecf\u5339\u914d\u7684\u5b57\u7b26\u5f00\u59cb\u5411\u56db\u5468\u5bfb\u627e\n    \u81ea\u884c\u6253\u5370current,\u4e0e\u4e0a\u4f8b\u8fdb\u884c\u6bd4\u8f83\u5373\u53ef\u67e5\u770b\u51fa\u5dee\u5f02\n\"\"\"\nclass Solution:\n    def exist(self, board: List[List[str]], word: str) -&gt; bool:\n        h, w, n = len(board), len(board[0]), len(word)  # \u7f51\u683c\u5bbd\u9ad8\uff0c\u4e0eword\u957f\u5ea6\n        if h*w &lt; n:\n            return False\n        directions = [(-1, 0), (0, -1), (0, 1), (1, 0)] # \u56db\u5468\u5750\u6807\u5411\u91cf\n\n        def backtrack(i, j, k, current):    #  \u7f51\u683c\u7b2ci\u884cj\u5217\uff0cword\u7b2ck\u4e2a\u5b57\u7b26\uff0c\u5f53\u524d\u5df2\u5339\u914d\u7684\u7f51\u683c\n            # print(i, j, k, current)\n            if len(current) == n:   # \u82e5\u5f53\u524d\u5339\u914d\u4ee5\u5339\u914d\u6240\u6709\u5b57\u7b26\uff0c\u8fd4\u56detrue\n                return True\n\n            for direction in directions:    # \u5411\u56db\u5468\u5ef6\u4f38\n                new_i, new_j = i+direction[0], j+direction[1]\n                if -1 &lt; new_i &lt; h and -1 &lt; new_j &lt; w and board[new_i][new_j] == word[k] and (new_i, new_j) not in current :\n                    current.append((new_i, new_j))\n                    if backtrack(new_i, new_j, k+1, current):   # \u5bf9\u5f53\u524d\u7f51\u683c\u56db\u5468\u5339\u914d\u4e0b\u4e00\u4e2aword\u4e2d\u5b57\u7b26\n                        return True\n            current.pop()   # \u82e5\u56db\u5468\u4e0d\u5339\u914d\uff0c\u5219\u56de\u6eaf\n\n\n        for i in range(h):\n            for j in range(w):\n                if board[i][j] == word[0]:\n                    if backtrack(i, j, 1, [(i, j)]):\n                        return True\n        return False\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a224 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8673.37% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8651.48% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u5c06\u5217\u8868\u6539\u6362\u4e3a\u5b57\u5178\n\"\"\"\nclass Solution:\n    def exist(self, board: List[List[str]], word: str) -&gt; bool:\n        h, w, n = len(board), len(board[0]), len(word)  # \u7f51\u683c\u5bbd\u9ad8\uff0c\u4e0eword\u957f\u5ea6\n        if h*w &lt; n:\n            return False\n        directions = [(-1, 0), (0, -1), (0, 1), (1, 0)] # \u56db\u5468\u5750\u6807\u5411\u91cf\n        current = {}\n        def backtrack(i, j, k, ):    #  \u7f51\u683c\u7b2ci\u884cj\u5217\uff0cword\u7b2ck\u4e2a\u5b57\u7b26\uff0c\u5f53\u524d\u5df2\u5339\u914d\u7684\u7f51\u683c\n            # print(i, j, k, current)\n            if len(current) == n:   # \u82e5\u5f53\u524d\u5339\u914d\u4ee5\u5339\u914d\u6240\u6709\u5b57\u7b26\uff0c\u8fd4\u56detrue\n                return True\n\n            for direction in directions:    # \u5411\u56db\u5468\u5ef6\u4f38\n                new_i, new_j = i+direction[0], j+direction[1]\n                if -1 &lt; new_i &lt; h and -1 &lt; new_j &lt; w and board[new_i][new_j] == word[k] and (new_i, new_j) not in current:\n                    current[(new_i, new_j)] = 0\n                    if backtrack(new_i, new_j, k+1):   # \u5bf9\u5f53\u524d\u7f51\u683c\u56db\u5468\u5339\u914d\u4e0b\u4e00\u4e2aword\u4e2d\u5b57\u7b26\n                        return True\n            del current[(i, j)]   # \u82e5\u56db\u5468\u4e0d\u5339\u914d\uff0c\u5219\u56de\u6eaf\n\n        for i in range(h):\n            for j in range(w):\n                if board[i][j] == word[0]:\n                    current[(i, j)] = 0\n                    if backtrack(i, j, 1):\n                        return True\n        return False<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u548c\u4e00\u4e2a\u5355\u8bcd\uff0c\u627e\u51fa\u8be5\u5355\u8bcd\u662f\u5426\u5b58\u5728\u4e8e\u7f51\u683c\u4e2d\u3002 \u5355\u8bcd\u5fc5\u987b\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\uff0c\u901a\u8fc7\u76f8\u90bb\u7684\u5355\u5143\u683c\u5185\u7684\u5b57\u6bcd\u6784\u6210\uff0c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/746"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=746"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/746\/revisions"}],"predecessor-version":[{"id":747,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/746\/revisions\/747"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=746"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}