圆月山庄资源网 Design By www.vgjia.com

可以和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,八皇后

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com