{"id":2114,"date":"2022-01-18T15:59:07","date_gmt":"2022-01-18T07:59:07","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2114"},"modified":"2022-01-18T16:52:30","modified_gmt":"2022-01-18T08:52:30","slug":"leetcodeday37","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/18\/leetcodeday37\/","title":{"rendered":"leetcodeday37 &#8211;\u89e3\u6570\u72ec"},"content":{"rendered":"\n<p>\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u901a\u8fc7\u586b\u5145\u7a7a\u683c\u6765\u89e3\u51b3\u6570\u72ec\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u6570\u72ec\u7684\u89e3\u6cd5\u9700<strong>&nbsp;\u9075\u5faa\u5982\u4e0b\u89c4\u5219<\/strong>\uff1a<\/p>\n\n\n\n<ol><li>\u6570\u5b57&nbsp;<code>1-9<\/code>&nbsp;\u5728\u6bcf\u4e00\u884c\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002<\/li><li>\u6570\u5b57&nbsp;<code>1-9<\/code>&nbsp;\u5728\u6bcf\u4e00\u5217\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002<\/li><li>\u6570\u5b57&nbsp;<code>1-9<\/code>&nbsp;\u5728\u6bcf\u4e00\u4e2a\u4ee5\u7c97\u5b9e\u7ebf\u5206\u9694\u7684&nbsp;<code>3x3<\/code>&nbsp;\u5bab\u5185\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002\uff08\u8bf7\u53c2\u8003\u793a\u4f8b\u56fe\uff09<\/li><\/ol>\n\n\n\n<p>\u6570\u72ec\u90e8\u5206\u7a7a\u683c\u5185\u5df2\u586b\u5165\u4e86\u6570\u5b57\uff0c\u7a7a\u767d\u683c\u7528&nbsp;<code>'.'<\/code>&nbsp;\u8868\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>board = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"],[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"],[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"],[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"],[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"],[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"],[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"],[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"],[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]\n<strong>\u8f93\u51fa\uff1a<\/strong>[[\"5\",\"3\",\"4\",\"6\",\"7\",\"8\",\"9\",\"1\",\"2\"],[\"6\",\"7\",\"2\",\"1\",\"9\",\"5\",\"3\",\"4\",\"8\"],[\"1\",\"9\",\"8\",\"3\",\"4\",\"2\",\"5\",\"6\",\"7\"],[\"8\",\"5\",\"9\",\"7\",\"6\",\"1\",\"4\",\"2\",\"3\"],[\"4\",\"2\",\"6\",\"8\",\"5\",\"3\",\"7\",\"9\",\"1\"],[\"7\",\"1\",\"3\",\"9\",\"2\",\"4\",\"8\",\"5\",\"6\"],[\"9\",\"6\",\"1\",\"5\",\"3\",\"7\",\"2\",\"8\",\"4\"],[\"2\",\"8\",\"7\",\"4\",\"1\",\"9\",\"6\",\"3\",\"5\"],[\"3\",\"4\",\"5\",\"2\",\"8\",\"6\",\"1\",\"7\",\"9\"]]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u8f93\u5165\u7684\u6570\u72ec\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u552f\u4e00\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u5982\u4e0b\u6240\u793a\uff1a\n\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>board.length == 9<\/code><\/li><li><code>board[i].length == 9<\/code><\/li><li><code>board[i][j]<\/code>&nbsp;\u662f\u4e00\u4f4d\u6570\u5b57\u6216\u8005&nbsp;<code>'.'<\/code><\/li><li>\u9898\u76ee\u6570\u636e&nbsp;<strong>\u4fdd\u8bc1<\/strong>&nbsp;\u8f93\u5165\u6570\u72ec\u4ec5\u6709\u4e00\u4e2a\u89e3<\/li><\/ul>\n\n\n\n<p>\u601d\u8def\uff1a\u56de\u6eaf\u6cd5\uff1a\u901a\u4fd7\u7406\u89e3\u5c31\u662f\u5982\u679cboard[i][j]=VALUE\u4e0d\u6ee1\u8db3\u6761\u4ef6\u5c31\u56de\u9000\u5230\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u91cd\u65b0\u9009\u62e9\u3002<\/p>\n\n\n\n<p>\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\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\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\u8fd9\u79cd\u8d70\u4e0d\u901a\u5c31\u9000\u56de\u518d\u8d70\u7684\u6280\u672f\u4e3a\u56de\u6eaf\u6cd5\uff0c\u800c\u6ee1\u8db3\u56de\u6eaf\u6761\u4ef6\u7684\u67d0\u4e2a\u72b6\u6001\u7684\u70b9\u79f0\u4e3a\u201c\u56de\u6eaf\u70b9\u201d\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>\u4ee3\u7801\u5b9e\u73b0\uff1a\uff08\u9012\u5f52\uff09<\/strong>\u3010\u53c2\u8003\u4e86\u89e3\u9898\u601d\u8def\u3011<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># &#91;37] \u89e3\u6570\u72ec\n#\n#\u56de\u6eaf\u6cd5\n\"\"\"\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\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\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\n\u8fd9\u79cd\u8d70\u4e0d\u901a\u5c31\u9000\u56de\u518d\u8d70\u7684\u6280\u672f\u4e3a\u56de\u6eaf\u6cd5\uff0c\n\u800c\u6ee1\u8db3\u56de\u6eaf\u6761\u4ef6\u7684\u67d0\u4e2a\u72b6\u6001\u7684\u70b9\u79f0\u4e3a\u201c\u56de\u6eaf\u70b9\u201d\u3002\n\n\"\"\"\n# @lc code=start\nclass Solution:\n    def solveSudoku(self, board)-&gt;None:\n        \"\"\"\n        Do not return anything, modify board in-place instead.\n        \"\"\"\n        #\u5224\u65ad\u6539\u884c\u3001\u5217\u30013*3\u5c0f\u683c\u5b50\u662f\u5426\u6ee1\u8db3\u6570\u72ec\u89c4\u5219\uff1a\n\n        def isRowSafe(row,value):\n            for i in range(9):\n                if board&#91;row]&#91;i]==value:\n                    return False\n            return True\n    \n        def isColSafe(col,value):\n            for i in range(9):\n                if board&#91;i]&#91;col]==value:\n                    return False\n            return True\n        \n        def isSmallboxSafe(row,col,value):\n            inirow=row\/\/3*3\n            inicol=col\/\/3*3\n            for i in range(3):\n                for j in range(3):\n                    if board&#91;i+inirow]&#91;j+inicol]==value:\n                        return False  \n            return True\n        #\u5224\u65ad\u8be5\u4f4d\u7f6e\u662f\u5426\u53ef\u884c\n        def isSafe(row,col,value):\n            return isRowSafe(row,value) and isColSafe(col,value) and isSmallboxSafe(row,col,value)                 \n        #\u89e3\u6570\u72ec\uff0c\u7ed3\u675f\u6761\u4ef6\n        def solve(row,col):\n            if row==8 and col ==9:\n                return True\n            if col ==9:\n                col=0\n                row+=1\n            if board&#91;row]&#91;col]!=\".\":\n                return solve(row,col+1)\n            for i in range(1,10):\n                if isSafe(row,col,str(i)):\n                    i=str(i)\n                    board&#91;row]&#91;col] = i\n                    if solve(row, col+1):\n                        return board\n           #\u56de\u6eaf\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001\uff08\u4e5f\u5c31\u662f\u524d\u4e00\u4e2asolve\uff09\n            board&#91;row]&#91;col]=\".\"\n            return False\n        solve(0,0)\n        print(board)<\/code><\/pre>\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-236.png\" alt=\"\" class=\"wp-image-2118\" width=\"464\" height=\"172\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-236.png 632w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-236-300x111.png 300w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u901a\u8fc7\u586b\u5145\u7a7a\u683c\u6765\u89e3\u51b3\u6570\u72ec\u95ee\u9898\u3002 \u6570\u72ec\u7684\u89e3\u6cd5\u9700&nbsp;\u9075\u5faa\u5982\u4e0b\u89c4\u5219\uff1a \u6570\u5b57&nbsp;1-9&#038;n &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/18\/leetcodeday37\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday37 &#8211;\u89e3\u6570\u72ec<\/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\/2114"}],"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=2114"}],"version-history":[{"count":5,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2114\/revisions"}],"predecessor-version":[{"id":2131,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2114\/revisions\/2131"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2114"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}