{"id":2156,"date":"2022-01-20T14:00:33","date_gmt":"2022-01-20T06:00:33","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2156"},"modified":"2022-01-20T14:00:35","modified_gmt":"2022-01-20T06:00:35","slug":"backtracking","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/20\/backtracking\/","title":{"rendered":"\u56de\u6eaf\u7b97\u6cd5"},"content":{"rendered":"\n<p>\u53c2\u8003\uff1a<a href=\"https:\/\/programmercarl.com\/\">https:\/\/programmercarl.com\/<\/a><\/p>\n\n\n\n<p>\u56de\u6eaf\u6cd5\uff0c\u4e00\u822c\u53ef\u4ee5\u89e3\u51b3\u5982\u4e0b\u51e0\u79cd\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul><li>\u7ec4\u5408\u95ee\u9898\uff1aN\u4e2a\u6570\u91cc\u9762\u6309\u4e00\u5b9a\u89c4\u5219\u627e\u51fak\u4e2a\u6570\u7684\u96c6\u5408<\/li><li>\u5207\u5272\u95ee\u9898\uff1a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6309\u4e00\u5b9a\u89c4\u5219\u6709\u51e0\u79cd\u5207\u5272\u65b9\u5f0f<\/li><li>\u5b50\u96c6\u95ee\u9898\uff1a\u4e00\u4e2aN\u4e2a\u6570\u7684\u96c6\u5408\u91cc\u6709\u591a\u5c11\u7b26\u5408\u6761\u4ef6\u7684\u5b50\u96c6<\/li><li>\u6392\u5217\u95ee\u9898\uff1aN\u4e2a\u6570\u6309\u4e00\u5b9a\u89c4\u5219\u5168\u6392\u5217\uff0c\u6709\u51e0\u79cd\u6392\u5217\u65b9\u5f0f<\/li><li>\u68cb\u76d8\u95ee\u9898\uff1aN\u7687\u540e\uff0c\u89e3\u6570\u72ec\u7b49\u7b49<\/li><\/ul>\n\n\n\n<p>        <strong>\u56de\u6eaf\u6cd5\u89e3\u51b3\u7684\u95ee\u9898\u90fd\u53ef\u4ee5\u62bd\u8c61\u4e3a\u6811\u5f62\u7ed3\u6784<\/strong>\uff0c\u662f\u7684\uff0c\u6211\u6307\u7684\u662f\u6240\u6709\u56de\u6eaf\u6cd5\u7684\u95ee\u9898\u90fd\u53ef\u4ee5\u62bd\u8c61\u4e3a\u6811\u5f62\u7ed3\u6784\uff01\u56e0\u4e3a\u56de\u6eaf\u6cd5\u89e3\u51b3\u7684\u90fd\u662f\u5728\u96c6\u5408\u4e2d\u9012\u5f52\u67e5\u627e\u5b50\u96c6\uff0c<strong>\u96c6\u5408\u7684\u5927\u5c0f\u5c31\u6784\u6210\u4e86\u6811\u7684\u5bbd\u5ea6\uff0c\u9012\u5f52\u7684\u6df1\u5ea6\uff0c\u90fd\u6784\u6210\u7684\u6811\u7684\u6df1\u5ea6<\/strong>\u3002\u9012\u5f52\u5c31\u8981\u6709\u7ec8\u6b62\u6761\u4ef6\uff0c\u6240\u4ee5\u5fc5\u7136\u662f\u4e00\u68f5\u9ad8\u5ea6\u6709\u9650\u7684\u6811\uff08N\u53c9\u6811\uff09\u3002<\/p>\n\n\n\n<ul><li>\u56de\u6eaf\u51fd\u6570\u6a21\u677f\u8fd4\u56de\u503c\u4ee5\u53ca\u53c2\u6570<\/li><\/ul>\n\n\n\n<p>\u5728\u56de\u6eaf\u7b97\u6cd5\u4e2d\uff0c\u6211\u7684\u4e60\u60ef\u662f\u51fd\u6570\u8d77\u540d\u5b57\u4e3abacktracking\uff0c\u8fd9\u4e2a\u8d77\u540d\u5927\u5bb6\u968f\u610f\u3002<\/p>\n\n\n\n<p>\u56de\u6eaf\u7b97\u6cd5\u4e2d\u51fd\u6570\u8fd4\u56de\u503c\u4e00\u822c\u4e3avoid\u3002<\/p>\n\n\n\n<p>\u518d\u6765\u770b\u4e00\u4e0b\u53c2\u6570\uff0c\u56e0\u4e3a\u56de\u6eaf\u7b97\u6cd5\u9700\u8981\u7684\u53c2\u6570\u53ef\u4e0d\u50cf\u4e8c\u53c9\u6811\u9012\u5f52\u7684\u65f6\u5019\u90a3\u4e48\u5bb9\u6613\u4e00\u6b21\u6027\u786e\u5b9a\u4e0b\u6765\uff0c\u6240\u4ee5\u4e00\u822c\u662f\u5148\u5199\u903b\u8f91\uff0c\u7136\u540e\u9700\u8981\u4ec0\u4e48\u53c2\u6570\uff0c\u5c31\u586b\u4ec0\u4e48\u53c2\u6570\u3002<\/p>\n\n\n\n<ul><li>\u56de\u6eaf\u51fd\u6570\u7ec8\u6b62\u6761\u4ef6<\/li><\/ul>\n\n\n\n<p>        \u65e2\u7136\u662f\u6811\u5f62\u7ed3\u6784\uff0c\u90a3\u4e48\u6211\u4eec\u5728\u8bb2\u89e3<a rel=\"noreferrer noopener\" href=\"https:\/\/programmercarl.com\/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html\" target=\"_blank\">\u4e8c\u53c9\u6811\u7684\u9012\u5f52&nbsp;(opens new window)<\/a>\u7684\u65f6\u5019\uff0c\u5c31\u77e5\u9053\u904d\u5386\u6811\u5f62\u7ed3\u6784\u4e00\u5b9a\u8981\u6709\u7ec8\u6b62\u6761\u4ef6\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u56de\u6eaf\u4e5f\u6709\u8981\u7ec8\u6b62\u6761\u4ef6\u3002<\/p>\n\n\n\n<p>        \u4ec0\u4e48\u65f6\u5019\u8fbe\u5230\u4e86\u7ec8\u6b62\u6761\u4ef6\uff0c\u6811\u4e2d\u5c31\u53ef\u4ee5\u770b\u51fa\uff0c\u4e00\u822c\u6765\u8bf4\u641c\u5230\u53f6\u5b50\u8282\u70b9\u4e86\uff0c\u4e5f\u5c31\u627e\u5230\u4e86\u6ee1\u8db3\u6761\u4ef6\u7684\u4e00\u6761\u7b54\u6848\uff0c\u628a\u8fd9\u4e2a\u7b54\u6848\u5b58\u653e\u8d77\u6765\uff0c\u5e76\u7ed3\u675f\u672c\u5c42\u9012\u5f52\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u56de\u6eaf\u51fd\u6570\u7ec8\u6b62\u6761\u4ef6\u4f2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (\u7ec8\u6b62\u6761\u4ef6) {\n    \u5b58\u653e\u7ed3\u679c;\n    return;\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-244-1024x467.png\" alt=\"\" class=\"wp-image-2162\" width=\"523\" height=\"238\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-244-1024x467.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-244-300x137.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-244-768x350.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-244.png 1138w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/figure><\/div>\n\n\n\n<p>\u56de\u6eaf\u51fd\u6570\u904d\u5386\u8fc7\u7a0b\u4f2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (\u9009\u62e9\uff1a\u672c\u5c42\u96c6\u5408\u4e2d\u5143\u7d20\uff08\u6811\u4e2d\u8282\u70b9\u5b69\u5b50\u7684\u6570\u91cf\u5c31\u662f\u96c6\u5408\u7684\u5927\u5c0f\uff09) {\n    \u5904\u7406\u8282\u70b9;\n    backtracking(\u8def\u5f84\uff0c\u9009\u62e9\u5217\u8868); \/\/ \u9012\u5f52\n    \u56de\u6eaf\uff0c\u64a4\u9500\u5904\u7406\u7ed3\u679c\n}<\/code><\/pre>\n\n\n\n<p>for\u5faa\u73af\u5c31\u662f\u904d\u5386\u96c6\u5408\u533a\u95f4\uff0c\u53ef\u4ee5\u7406\u89e3\u4e00\u4e2a\u8282\u70b9\u6709\u591a\u5c11\u4e2a\u5b69\u5b50\uff0c\u8fd9\u4e2afor\u5faa\u73af\u5c31\u6267\u884c\u591a\u5c11\u6b21\u3002 backtracking\u8fd9\u91cc\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\uff0c\u5b9e\u73b0\u9012\u5f52\u3002<\/p>\n\n\n\n<p>\u5927\u5bb6\u53ef\u4ee5\u4ece\u56fe\u4e2d\u770b\u51fa<strong>for\u5faa\u73af\u53ef\u4ee5\u7406\u89e3\u662f\u6a2a\u5411\u904d\u5386\uff0cbacktracking\uff08\u9012\u5f52\uff09\u5c31\u662f\u7eb5\u5411\u904d\u5386<\/strong>\uff0c\u8fd9\u6837\u5c31\u628a\u8fd9\u68f5\u6811\u5168\u904d\u5386\u5b8c\u4e86\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u641c\u7d22\u53f6\u5b50\u8282\u70b9\u5c31\u662f\u627e\u7684\u5176\u4e2d\u4e00\u4e2a\u7ed3\u679c\u4e86\u3002<\/p>\n\n\n\n<p>\u5206\u6790\u5b8c\u8fc7\u7a0b\uff0c\u56de\u6eaf\u7b97\u6cd5\u6a21\u677f\u6846\u67b6\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void backtracking(\u53c2\u6570) {\n    if (\u7ec8\u6b62\u6761\u4ef6) {\n        \u5b58\u653e\u7ed3\u679c;\n        return;\n    }\n\n    for (\u9009\u62e9\uff1a\u672c\u5c42\u96c6\u5408\u4e2d\u5143\u7d20\uff08\u6811\u4e2d\u8282\u70b9\u5b69\u5b50\u7684\u6570\u91cf\u5c31\u662f\u96c6\u5408\u7684\u5927\u5c0f\uff09) {\n        \u5904\u7406\u8282\u70b9;\n        backtracking(\u8def\u5f84\uff0c\u9009\u62e9\u5217\u8868); \/\/ \u9012\u5f52\n        \u56de\u6eaf\uff0c\u64a4\u9500\u5904\u7406\u7ed3\u679c\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>\u8fd9\u4efd\u6a21\u677f\u5f88\u91cd\u8981\uff0c\u540e\u9762\u505a\u56de\u6eaf\u6cd5\u7684\u9898\u76ee\u90fd\u9760\u5b83\u4e86\uff01<\/strong><\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>\u5b9e\u4f8b\uff1a \u89e3\u6570\u72ec<\/strong> \uff08\u56de\u6eaf\u6cd5\uff09<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-245.png\" alt=\"\" class=\"wp-image-2167\" width=\"288\" height=\"281\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-245.png 514w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-245-300x292.png 300w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u56de\u6eaf\u6cd5\r\n\"\"\"\r\n\u56de\u6eaf\u6cd5\uff08\u63a2\u7d22\u4e0e\u56de\u6eaf\u6cd5\uff09\u662f\u4e00\u79cd\u9009\u4f18\u641c\u7d22\u6cd5\uff0c\u53c8\u79f0\u4e3a\u8bd5\u63a2\u6cd5\uff0c\r\n\u6309\u9009\u4f18\u6761\u4ef6\u5411\u524d\u641c\u7d22\uff0c\u4ee5\u8fbe\u5230\u76ee\u6807\u3002\u4f46\u5f53\u63a2\u7d22\u5230\u67d0\u4e00\u6b65\u65f6\uff0c\r\n\u53d1\u73b0\u539f\u5148\u9009\u62e9\u5e76\u4e0d\u4f18\u6216\u8fbe\u4e0d\u5230\u76ee\u6807\uff0c\u5c31\u9000\u56de\u4e00\u6b65\u91cd\u65b0\u9009\u62e9\uff0c\r\n\u8fd9\u79cd\u8d70\u4e0d\u901a\u5c31\u9000\u56de\u518d\u8d70\u7684\u6280\u672f\u4e3a\u56de\u6eaf\u6cd5\uff0c\r\n\u800c\u6ee1\u8db3\u56de\u6eaf\u6761\u4ef6\u7684\u67d0\u4e2a\u72b6\u6001\u7684\u70b9\u79f0\u4e3a\u201c\u56de\u6eaf\u70b9\u201d\u3002\r\n\r\n\"\"\"\r\n# @lc code=start\r\nclass Solution:\r\n    def solveSudoku(self, board)->None:\r\n        \"\"\"\r\n        Do not return anything, modify board in-place instead.\r\n        \"\"\"\r\n        #\u5224\u65ad\u6539\u884c\u3001\u5217\u30013*3\u5c0f\u683c\u5b50\u662f\u5426\u6ee1\u8db3\u6570\u72ec\u89c4\u5219\uff1a\r\n\r\n        def isRowSafe(row,value):\r\n            for i in range(9):\r\n                if board&#91;row]&#91;i]==value:\r\n                    return False\r\n            return True\r\n    \r\n        def isColSafe(col,value):\r\n            for i in range(9):\r\n                if board&#91;i]&#91;col]==value:\r\n                    return False\r\n            return True\r\n        \r\n        def isSmallboxSafe(row,col,value):\r\n            inirow=row\/\/3*3\r\n            inicol=col\/\/3*3\r\n            for i in range(3):\r\n                for j in range(3):\r\n                    if board&#91;i+inirow]&#91;j+inicol]==value:\r\n                        return False  \r\n            return True\r\n        #\u5224\u65ad\u8be5\u4f4d\u7f6e\u662f\u5426\u53ef\u884c\r\n        def isSafe(row,col,value):\r\n            return isRowSafe(row,value) and isColSafe(col,value) and isSmallboxSafe(row,col,value)                 \r\n        #\u89e3\u6570\u72ec\uff0c\u7ed3\u675f\u6761\u4ef6\uff08\u56de\u6eaf\u6cd5\uff0c\u9012\u5f52\u8c03\u7528\uff09\r\n        def solve(row,col):\r\n            if row==8 and col ==9:\r\n                return True\r\n            if col ==9:\r\n                col=0\r\n                row+=1\r\n            if board&#91;row]&#91;col]!=\".\":\r\n                return solve(row,col+1)\r\n            for i in range(1,10):\r\n                if isSafe(row,col,str(i)):\r\n                    i=str(i)\r\n                    board&#91;row]&#91;col] = i\r\n                    if solve(row, col+1):\r\n                        return board\r\n            board&#91;row]&#91;col]=\".\"\r\n            return False\r\n        solve(0,0)\r\n        print(board)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\uff1ahttps:\/\/programmercarl.com\/ \u56de\u6eaf\u6cd5\uff0c\u4e00\u822c\u53ef\u4ee5\u89e3\u51b3\u5982\u4e0b\u51e0\u79cd\u95ee\u9898\uff1a \u7ec4\u5408\u95ee\u9898\uff1aN &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/20\/backtracking\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u56de\u6eaf\u7b97\u6cd5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2156"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2156"}],"version-history":[{"count":11,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2156\/revisions"}],"predecessor-version":[{"id":2169,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2156\/revisions\/2169"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2156"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}