{"id":204688,"date":"2025-05-29T10:29:44","date_gmt":"2025-05-29T02:29:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204688\/"},"modified":"2025-05-29T10:29:44","modified_gmt":"2025-05-29T02:29:44","slug":"%e7%94%a8-python-%e6%b1%82%e8%a7%a3%e6%95%b0%e7%8b%ac","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204688\/","title":{"rendered":"\u7528 Python \u6c42\u89e3\u6570\u72ec"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7528 Python \u6c42\u89e3\u6570\u72ec<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u7528 Python \u6c42\u89e3\u6570\u72ec\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241113\/173148035467344b22a6cd3.jpg\" class=\"aligncenter\"><\/p>\n<p>\u521b\u5efa\u6570\u72ec\u6c42\u89e3\u5668\u662f\u719f\u6089\u9012\u5f52\u56de\u6eaf\u548c\u7b97\u6cd5\u6c42\u89e3\u7684\u597d\u65b9\u6cd5\u3002\u5728\u8fd9\u7bc7\u535a\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u6211\u521b\u5efa\u7684\u547d\u4ee4\u884c\u6570\u72ec\u6e38\u620f\u9879\u76ee\u4e2d\u7684\u4e00\u4e9b\u8f85\u52a9\u51fd\u6570\uff0c\u4ee5\u6f14\u793a\u8fd9\u4e9b\u65b9\u6cd5\u3002\u8be5\u6587\u4ef6\u5305\u542b\u6709\u52a9\u4e8e\u89e3\u51b3\u6570\u72ec\u8c1c\u9898\u7684\u57fa\u672c\u8f85\u52a9\u51fd\u6570\u3002\u6211\u4eec\u5c06\u5206\u89e3\u5173\u952e\u51fd\u6570\uff1ais_valid\u3001find_empty \u548csolve\u3002<\/p>\n<p>\u68c0\u67e5\u53f7\u7801\u7684\u6709\u6548\u6027<br \/> is_valid \u51fd\u6570\u6839\u636e\u6570\u72ec\u89c4\u5219\u68c0\u67e5\u5728\u7ed9\u5b9a\u5355\u5143\u683c\u4e2d\u653e\u7f6e\u7279\u5b9a\u6570\u5b57\u662f\u5426\u6709\u6548\u3002<\/p>\n<pre>def is_valid(board, row, col, num):\n    # check if the number is not present in the same row and column\n    if num in board[row] or num in [board[i][col] for i in range(9)]:\n        return false\n\n    start_row, start_col = 3 * (row \/\/ 3), 3 * (col \/\/ 3)\n    for i in range(start_row, start_row + 3):\n        for j in range(start_col, start_col + 3):\n            if board[i][j] == num:\n                return false\n    return true\n<\/pre>\n<p><strong>\u884c\u548c\u5217\u68c0\u67e5<\/strong>\uff1a\u786e\u4fdd\u6570\u5b57\u4e0d\u5b58\u5728\u4e8e\u540c\u4e00\u884c\u6216\u5217\u4e2d\u3002<br \/><strong>\u5b50\u7f51\u683c\u68c0\u67e5<\/strong>\uff1a\u786e\u4fdd\u8be5\u6570\u5b57\u4e0d\u5b58\u5728\u4e8e 3&#215;3 \u5b50\u7f51\u683c\u4e2d\u3002<\/p>\n<p>find_empty \u51fd\u6570\u5b9a\u4f4d\u68cb\u76d8\u4e0a\u7684\u4e0b\u4e00\u4e2a\u7a7a\u5355\u5143\u683c\uff08\u7528 0 \u8868\u793a\uff09\u3002<\/p>\n<pre>def find_empty(board):\n    for i in range(9):\n        for j in range(9):\n            if board[i][j] == 0:\n                return (i, j)\n    return none\n<\/pre>\n<p><strong>\u8fed\u4ee3<\/strong>\uff1a\u8fed\u4ee3\u68cb\u76d8\u4ee5\u67e5\u627e\u7a7a\u5355\u5143\u683c\u3002<br \/><strong>\u8fd4\u56de<\/strong>\uff1a\u8fd4\u56de\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u7a7a\u5355\u5143\u683c\u7684\u5750\u6807\uff0c\u5982\u679c\u68cb\u76d8\u5df2\u6ee1\uff0c\u5219\u8fd4\u56de none\u3002<\/p>\n<p>\u89e3\u51b3\u51fd\u6570\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u6570\u72ec\u96be\u9898\u3002<\/p>\n<pre>def solve(board):\n    empty_cell = find_empty(board)\n    # Board is solved\n    if not empty_cell:\n        return board\n\n    row, col = empty_cell\n\n    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n    random.shuffle(numbers)\n\n    for num in numbers:\n        if is_valid(board, row, col, num):\n            board[row][col] = num\n            if solve(board):\n                return board\n            # Backtrack if current placement doesn't lead to a solution\n            board[row][col] = 0\n        # No valid number for current empty cell\n    return False\n<\/pre>\n<p><strong>\u67e5\u627e\u7a7a\u5355\u5143\u683c<\/strong>\uff1a\u4f7f\u7528 find_empty \u5b9a\u4f4d\u4e0b\u4e00\u4e2a\u7a7a\u5355\u5143\u683c\u3002<br \/> \u56de\u6eaf\uff1a\u5c1d\u8bd5\u5c06\u6570\u5b57 1-9 \u653e\u5165\u7a7a\u5355\u5143\u683c\u4e2d\uff0c\u4f7f\u7528 is_valid \u68c0\u67e5\u6709\u6548\u6027\u3002<\/p>\n<p><strong>\u9012\u5f52\u6c42\u89e3<\/strong>\uff1a\u9012\u5f52\u5c1d\u8bd5\u6c42\u89e3\u68cb\u76d8\u3002\u5982\u679c\u67d0\u4e2a\u4f4d\u7f6e\u4ea7\u751f\u4e86\u89e3\u51b3\u65b9\u6848\uff0c\u5219\u4f1a\u8fd4\u56de\u5df2\u89e3\u51b3\u7684\u68cb\u76d8\u3002<br \/> \u56de\u6eaf\uff1a\u5982\u679c\u653e\u7f6e\u6ca1\u6709\u4ea7\u751f\u89e3\u51b3\u65b9\u6848\uff0c\u5219\u4f1a\u91cd\u7f6e\u5355\u5143\u683c\u5e76\u5c1d\u8bd5\u4e0b\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<p>\u8f85\u52a9\u51fd\u6570\u5bf9\u4e8e\u6570\u72ec\u89e3\u7b97\u5668\u7684\u529f\u80fd\u81f3\u5173\u91cd\u8981\u3002 is_valid \u51fd\u6570\u786e\u4fdd\u9075\u5faa\u6570\u72ec\u89c4\u5219\uff0cfind_empty \u5e2e\u52a9\u5b9a\u4f4d\u4e0b\u4e00\u4e2a\u8981\u586b\u5145\u7684\u5355\u5143\u683c\uff0csolve \u4f7f\u7528\u9012\u5f52\u56de\u6eaf\u6765\u67e5\u627e\u89e3\u51b3\u65b9\u6848\u3002\u4e86\u89e3\u8fd9\u4e9b\u8f85\u52a9\u51fd\u6570\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u89e3\u51b3\u6570\u72ec\u8c1c\u9898\u80cc\u540e\u7684\u903b\u8f91\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u7528 Python \u6c42\u89e3\u6570\u72ec\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 Python \u6c42\u89e3\u6570\u72ec IT&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204688","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204688"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204688\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}