1 Star 0 Fork 0

Awu-Miao/learn-z3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
5-magic-square.py 926 Bytes
一键复制 编辑 原始数据 按行查看 历史
PwnFunction 提交于 2022-07-13 12:03 . updated README.md
#!/usr/bin/env python3
import z3
from itertools import chain
"""
A square array of numbers, usually positive integers, is called a magic square
if the sums of the numbers in each row, each column, and both main diagonals are the same.
Magic square of 15 looks like:
8 1 6
3 5 7
4 9 2
"""
x = [[z3.Int(f'x_{i}_{j}') for i in range(3)] for j in range(3)]
range_c = [z3.And(i >= 1, i <= 9) for i in chain(*x)]
unique_c = [z3.Distinct([i for i in chain(*x)])]
row_c = [sum(i) == 15 for i in x]
column_c = [sum(list(chain(*x))[i::3]) == 15 for i in range(3)]
diagonal_c = [x[0][0]+x[1][1]+x[2][2] == 15,
x[0][2]+x[1][1]+x[2][0] == 15]
magic_c = range_c + unique_c + row_c + column_c + diagonal_c
solver = z3.Solver()
solver.add(magic_c)
if solver.check() == z3.sat:
m = solver.model()
r = [[m.evaluate(x[i][j]) for i in range(3)] for j in range(3)]
for i in r:
print(' '.join(map(str, i)))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Awu-Miao/learn-z3.git
[email protected]:Awu-Miao/learn-z3.git
Awu-Miao
learn-z3
learn-z3
main

搜索帮助