python 示例分享---逻辑推理编程解决八皇后
2019/7/13 21:48:54
本文主要是介绍python 示例分享---逻辑推理编程解决八皇后,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
可以和Haskell , Prolog 一样做到模式匹配,
建立逻辑推到规则,描述问题,得出答案。
from pyDatalog import pyDatalog pyDatalog.create_atoms( 'N, N1, X, Y, X0, X1, X2, X3, X4, X5, X6, X7' ) pyDatalog.create_atoms( 'ok, queens, next_queen, pred, pred2' ) size = 8 ok( X1, N, X2 ) <= ( X1 != X2 ) & ( X1 != X2 + N ) & ( X1 != X2 - N ) pred( N, N1 ) <= ( N > 1 ) & ( N1 == N - 1 ) queens( 1, X ) <= ( X1._in( range( size ) ) ) & ( X1 == X[0] ) queens( N, X ) <= pred( N, N1 ) & queens( N1, X[:-1] ) & next_queen( N, X ) pred2( N, N1 ) <= ( N > 2 ) & ( N1 == N - 1 ) next_queen( 2, X ) <= ( X1._in( range( 8 ) ) ) & ok( X[0], 1, X1 ) & ( X1 == X[1] ) next_queen( N, X ) <= pred2( N, N1 ) & next_queen( N1, X[1:] ) & ok( X[0], N1, X[-1] ) print( queens( size, ( X0, X1, X2, X3, X4, X5, X6, X7 ) ) )
这篇关于python 示例分享---逻辑推理编程解决八皇后的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享