{"id":1318,"date":"2023-03-25T10:12:45","date_gmt":"2023-03-25T02:12:45","guid":{"rendered":""},"modified":"2023-03-25T10:12:45","modified_gmt":"2023-03-25T02:12:45","slug":"\u542f\u53d1\u5f0f\u641c\u7d22","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1318.html","title":{"rendered":"\u542f\u53d1\u5f0f\u641c\u7d22"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

\u542f\u53d1\u5f0f\u641c\u7d22<\/h1>\n

\u542f\u53d1\u5f0f\u641c\u7d22\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u542f\u53d1\u5f0f\u641c\u7d22\u5728\u4eba\u5de5\u667a\u80fd\u4e2d\u8d77\u7740\u5173\u952e\u4f5c\u7528\u3002 \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u6765\u8be6\u7ec6\u5730\u4e86\u89e3\u5b83\u3002\n <\/div>\n

AI\u4e2d\u542f\u53d1\u5f0f\u641c\u7d22\u7684\u6982\u5ff5<\/h2>\n
\n \u542f\u53d1\u5f0f\u662f\u4e00\u6761\u7ecf\u9a8c\u6cd5\u5219\uff0c\u5b83\u5c06\u6211\u4eec\u5f15\u5411\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u4eba\u5de5\u667a\u80fd\u4e2d\u7684\u5927\u591a\u6570\u95ee\u9898\u5177\u6709\u6307\u6570\u6027\uff0c\u5e76\u4e14\u6709\u8bb8\u591a\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5e76\u4e0d\u786e\u5207\u77e5\u9053\u54ea\u4e9b\u89e3\u51b3\u65b9\u6848\u662f\u6b63\u786e\u7684\uff0c\u68c0\u67e5\u6240\u6709\u89e3\u51b3\u65b9\u6848\u4f1a\u975e\u5e38\u6602\u8d35\u3002\n <\/div>\n
\n \u56e0\u6b64\uff0c\u542f\u53d1\u5f0f\u7684\u4f7f\u7528\u7f29\u5c0f\u4e86\u641c\u7d22\u89e3\u51b3\u65b9\u6848\u7684\u8303\u56f4\u5e76\u6d88\u9664\u4e86\u9519\u8bef\u7684\u9009\u9879\u3002 \u542f\u53d1\u5f0f\u5f15\u5bfc\u641c\u7d22\u7a7a\u95f4\u4e2d\u7684\u641c\u7d22\u7684\u65b9\u6cd5\u79f0\u4e3a\u542f\u53d1\u5f0f\u641c\u7d22\u3002 \u542f\u53d1\u5f0f\u6280\u672f\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u4f7f\u7528\u5b83\u4eec\u65f6\u53ef\u4ee5\u63d0\u9ad8\u641c\u7d22\u6548\u7387\u3002\n <\/div>\n

\u4e0d\u77e5\u60c5\u548c\u77e5\u60c5\u641c\u7d22\u4e4b\u95f4\u7684\u533a\u522b<\/h2>\n
\n \u6709\u4e24\u79cd\u63a7\u5236\u7b56\u7565\u6216\u641c\u7d22\u6280\u672f:\u4e0d\u77e5\u60c5\u548c\u77e5\u60c5\u3002\u8fd9\u91cc\u7ed9\u51fa\u7684\u8be6\u7ec6\u89e3\u91ca\u5982\u4e0b -\n <\/div>\n
\n \u4e0d\u77e5\u60c5\u7684\u641c\u7d22<\/strong>\u5b83\u4e5f\u88ab\u79f0\u4e3a\u76f2\u641c\u7d22\u6216\u76f2\u63a7\u5236\u7b56\u7565\u3002 \u5b83\u7684\u547d\u540d\u662f\u56e0\u4e3a\u53ea\u6709\u5173\u4e8e\u95ee\u9898\u5b9a\u4e49\u7684\u4fe1\u606f\uff0c\u5e76\u4e14\u6ca1\u6709\u5173\u4e8e\u72b6\u6001\u7684\u5176\u4ed6\u989d\u5916\u4fe1\u606f\u3002 \u8fd9\u79cd\u641c\u7d22\u6280\u672f\u5c06\u641c\u7d22\u6574\u4e2a\u72b6\u6001\u7a7a\u95f4\u4ee5\u83b7\u5f97\u89e3\u51b3\u65b9\u6848\u3002 \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22(BFS)\u548c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22(DFS)\u662f\u975e\u4fe1\u606f\u641c\u7d22\u7684\u793a\u4f8b\u3002\n <\/div>\n
\n \u77e5\u60c5\u641c\u7d22<\/strong>\u5b83\u4e5f\u88ab\u79f0\u4e3a\u542f\u53d1\u5f0f\u641c\u7d22\u6216\u542f\u53d1\u5f0f\u63a7\u5236\u7b56\u7565\u3002 \u5b83\u7684\u540d\u5b57\u662f\u56e0\u4e3a\u6709\u4e00\u4e9b\u989d\u5916\u7684\u72b6\u6001\u4fe1\u606f\u3002 \u8fd9\u4e9b\u989d\u5916\u7684\u4fe1\u606f\u5bf9\u8ba1\u7b97\u5b50\u8282\u70b9\u4e4b\u95f4\u7684\u504f\u597d\u4ee5\u4fbf\u63a2\u7d22\u548c\u6269\u5c55\u5f88\u6709\u7528\u3002 \u5c06\u4f1a\u6709\u4e0e\u6bcf\u4e2a\u8282\u70b9\u76f8\u5173\u7684\u542f\u53d1\u5f0f\u529f\u80fd\u3002 Best First Search(BFS)\uff0cA *\uff0cMean\u548cAnalysis\u662f\u77e5\u60c5\u641c\u7d22\u7684\u4f8b\u5b50\u3002\n <\/div>\n
\n \u7ea6\u675f\u6ee1\u8db3\u95ee\u9898(CSP)<\/strong>\u7ea6\u675f\u610f\u5473\u7740\u9650\u5236\u6216\u9650\u5236\u3002 \u5728\u4eba\u5de5\u667a\u80fd\u4e2d\uff0c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u662f\u4e00\u4e9b\u7ea6\u675f\u6761\u4ef6\u4e0b\u5fc5\u987b\u89e3\u51b3\u7684\u95ee\u9898\u3002 \u91cd\u70b9\u5fc5\u987b\u662f\u5728\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u65f6\u4e0d\u8981\u8fdd\u53cd\u7ea6\u675f\u6761\u4ef6\u3002 \u6700\u540e\uff0c\u5f53\u6211\u4eec\u8fbe\u6210\u6700\u7ec8\u89e3\u51b3\u65b9\u6848\u65f6\uff0cCSP\u5fc5\u987b\u9075\u5b88\u9650\u5236\u3002\n <\/div>\n

\u7ea6\u675f\u6ee1\u8db3\u89e3\u51b3\u7684\u73b0\u5b9e\u4e16\u754c\u95ee\u9898<\/h2>\n
\n \u524d\u9762\u7684\u90e8\u5206\u6d89\u53ca\u521b\u5efa\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u3002 \u73b0\u5728\u5c06\u5b83\u5e94\u7528\u4e8e\u73b0\u5b9e\u4e16\u754c\u7684\u95ee\u9898\u3002 \u901a\u8fc7\u7ea6\u675f\u6ee1\u8db3\u89e3\u51b3\u7684\u73b0\u5b9e\u4e16\u754c\u95ee\u9898\u7684\u4e00\u4e9b\u4f8b\u5b50\u5982\u4e0b -\n <\/div>\n
\n \u89e3\u51b3\u4ee3\u6570\u5173\u7cfb<\/strong>\n <\/div>\n
\n \u5728\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u5e2e\u52a9\u4e0b\uff0c\u53ef\u4ee5\u6c42\u89e3\u4ee3\u6570\u5173\u7cfb\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5c1d\u8bd5\u89e3\u51b3\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u6570\u5173\u7cfba * 2 = b\u3002 \u5b83\u4f1a\u5728\u6211\u4eec\u5b9a\u4e49\u7684\u8303\u56f4\u5185\u8fd4\u56dea\u548cb\u7684\u503c\u3002\n <\/div>\n
\n \u5b8c\u6210\u6b64Python\u7a0b\u5e8f\u540e\uff0c\u60a8\u5c06\u80fd\u591f\u7406\u89e3\u89e3\u51b3\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u57fa\u7840\u77e5\u8bc6\u3002\n <\/div>\n
\n \u8bf7\u6ce8\u610f\uff0c\u5728\u7f16\u5199\u7a0b\u5e8f\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5\u540d\u4e3apython-constraint\u7684Python\u5305\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
pip install python-constraint
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0b\u6b65\u9aa4\u5411\u60a8\u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u7ea6\u675f\u6ee1\u8db3\u6765\u89e3\u51b3\u4ee3\u6570\u5173\u7cfb\u7684Python\u7a0b\u5e8f\u3002\n <\/div>\n
\n \u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5bfc\u5165\u7ea6\u675f\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>constraint import <\/span>*
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aproblem()\u7684\u6a21\u5757\u5bf9\u8c61\uff0c\u5982\u4e0b\u6240\u793a -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
problem = Problem()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u5b9a\u4e49\u53d8\u91cf\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u91cc\u6709\u4e24\u4e2a\u53d8\u91cfa\u548cb\uff0c\u5e76\u4e14\u5c06\u5b9a\u4e4910\u4e3a\u5b83\u4eec\u7684\u8303\u56f4\uff0c\u8fd9\u610f\u5473\u7740\u5728\u524d10\u4e2a\u6570\u5b57\u8303\u56f4\u5185\u5f97\u5230\u89e3\u51b3\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
problem.addVariable<\/span>('a'<\/span>, range(10))
problem.addVariable<\/span>('b'<\/span>, range(10))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u63a5\u4e0b\u6765\uff0c\u5b9a\u4e49\u5e94\u7528\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u7279\u5b9a\u7ea6\u675f\u3002 \u8bf7\u6ce8\u610f\uff0c\u8fd9\u91cc\u4f7f\u7528\u7ea6\u675fa * 2 = b\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
problem.addConstraint<\/span>(lambda <\/span>a, b: a * 2 == b)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efagetSolution()\u6a21\u5757\u7684\u5bf9\u8c61 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
solutions = problem.getSolutions<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6700\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6253\u5370\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
print <\/span>(solutions)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u53ef\u4ee5\u89c2\u5bdf\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
[{'a'<\/span><\/span><\/span><\/span><\/span>: 4, 'b'<\/span><\/span><\/span><\/span><\/span>: 8}, {'a': 3, 'b': 6}, {'a': 2, 'b': 4}, {'a': 1, 'b': 2}, {'a': 0, 'b': 0}]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u9b54\u5e7b\u6b63\u65b9\u5f62<\/strong>\u4e00\u4e2a\u795e\u5947\u7684\u6b63\u65b9\u5f62\u662f\u4e00\u4e2a\u6b63\u65b9\u5f62\u7f51\u683c\u4e2d\u4e0d\u540c\u6570\u5b57(\u901a\u5e38\u662f\u6574\u6570)\u7684\u6392\u5217\uff0c\u5176\u4e2d\u6bcf\u884c\u548c\u6bcf\u5217\u4e2d\u7684\u6570\u5b57\u4ee5\u53ca\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6570\u5b57\u52a0\u8d77\u6765\u5c31\u662f\u6240\u8c13\u7684\u201c\u9b54\u672f\u5e38\u6570\u201d\u3002\n <\/div>\n
\n \u4ee5\u4e0b\u662f\u7528\u4e8e\u751f\u6210\u5e7b\u65b9\u7684\u7b80\u5355Python\u4ee3\u7801\u7684\u9010\u6b65\u6267\u884c -\n <\/div>\n
\n \u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3amagic_square\u7684\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>magic_square(matrix_ms):
   iSize = len(matrix_ms[0])
   sum_list = []
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0b\u4ee3\u7801\u663e\u793a\u4e86\u5782\u76f4\u65b9\u5757\u7684\u4ee3\u7801 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
for <\/span><\/span>col in <\/span>range(iSize):
   sum_list.append<\/span>(sum(row[col] for <\/span><\/span>row in <\/span>matrix_ms))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0b\u4ee3\u7801\u663e\u793a\u4e86\u6c34\u5e73\u65b9\u5757\u7684\u4ee3\u7801 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
sum_list.extend<\/span>([sum (lines) for <\/span><\/span>lines in <\/span>matrix_ms])
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6c34\u5e73\u65b9\u5757\u7684\u4ee3\u7801\u5b9e\u73b0 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
dlResult = 0
for <\/span><\/span>i in <\/span>range(0,iSize):
   dlResult +=matrix_ms[i][i]
sum_list.append<\/span>(dlResult)
drResult = 0
for <\/span><\/span>i in <\/span>range(iSize-1,-1,-1):
   drResult +=matrix_ms[i][i]
sum_list.append<\/span>(drResult)
if <\/span>len(set(sum_list))>1:
   return <\/span>False<\/span>
return True<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u7ed9\u51fa\u77e9\u9635\u7684\u503c\u5e76\u67e5\u770b\u8f93\u51fa\u7ed3\u679c -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
print(magic_square([[1,2,3], [4,5,6], [7,8,9]]))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u53ef\u4ee5\u89c2\u5bdf\u5230\u7531\u4e8e\u603b\u548c\u672a\u8fbe\u5230\u76f8\u540c\u6570\u5b57\uff0c\u8f93\u51fa\u5c06\u4e3aFalse\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
print(magic_square([[3,9,2], [3,5,7], [9,1,6]]))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u53ef\u4ee5\u89c2\u5bdf\u5230\u8f93\u51fa\u5c06\u4e3aTrue\uff0c\u56e0\u4e3a\u603b\u548c\u662f\u76f8\u540c\u7684\u6570\u5b57\uff0c\u537315\u3002\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"\u542f\u53d1\u5f0f\u641c\u7d22zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[],"class_list":["post-1318","post","type-post","status-publish","format-standard","hentry","category-rgznjc1"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1318"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1318"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1318\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}