{"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
\u542f\u53d1\u5f0f\u641c\u7d22\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n <\/body>AI\u4e2d\u542f\u53d1\u5f0f\u641c\u7d22\u7684\u6982\u5ff5<\/h2>\n
\u4e0d\u77e5\u60c5\u548c\u77e5\u60c5\u641c\u7d22\u4e4b\u95f4\u7684\u533a\u522b<\/h2>\n
\u7ea6\u675f\u6ee1\u8db3\u89e3\u51b3\u7684\u73b0\u5b9e\u4e16\u754c\u95ee\u9898<\/h2>\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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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<\/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}]}}