数据库约束解四皇后问题

人工智能中约束满足问题CSP

p=<x,c>

x = { x1,x2.....}表示n个变量x的集合

c={c1,c2...} 表示n个约束的集合

ci 为满足约束条件的x的取值集合

求解约束满足问题p,就是c的自然连接的结果

在老师的讲解下,稍微理解了一点,

一下用四皇后问题作为例子:希望需要的人可以参考一下




use master;

if db_id('fourQueen') is not null
   drop database fourQueen;
go
create database fourQueen;
go
use fourQueen;

--创建变量取值范围表
create table Value(
	row int
);
insert into Value values(1),(2),(3),(4);

--约束1第一行对第二行的约数
create table FirToSec(
	fir int,
	sec int
);
insert into FirToSec select a.row,b.row from Value a,Value b where a.row - b.row not in(0,1,-1);

--约数2第一行对第三行的约数
create table FirToThi(
	fir int,
	thi int
);
insert into FirToThi select a.row,b.row from Value a,Value b where a.row - b.row not in(0,2,-2);


--约数3第一行对第四行的约数
create table FirToFou(
	fir int,
	fou int
);
insert into FirToFou select a.row,b.row from Value a,Value b where a.row - b.row not in(0,3,-3);


--约数4第二行对第三行的约数
create table SecToThi(
	sec int,
	thi int
);
insert into SecToThi select a.row,b.row from Value a,Value b where a.row - b.row not in(0,1,-1);

--约数5第二行对第三行的约数
create table SecToFou(
	sec int,
	fou int
);
insert into SecToFou select a.row,b.row from Value a,Value b where a.row - b.row not in(0,2,-3);

--约数6第三行对第四行的约数
create table ThiToFou(
	thi int,
	Fou int
);
insert into ThiToFou select a.row,b.row from Value a,Value b where a.row - b.row not in(0,1,-1);

--求解的自然连接
select x1.fir,x1.sec,x6.thi,x6.Fou from FirToSec x1,FirToThi x2,FirToFou x3,SecToThi x4,SecToFou x5,ThiToFou x6
where x1.fir = x2.fir and x1.fir = x3.fir 
and x1.sec = x4.sec and x1.sec = x5.sec
and x2.thi = x4.thi and x2.thi = x6.thi
and x3.fou = x5.fou and x3.fou = x6.Fou;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值