n皇后的来了:
declare @i int,@j int,@n int,@sql nvarchar(max)
set @n=12
set @sql='declare @c table ('
set @i=1
while @i<@n
begin
set @sql=@sql+'c'+cast(@i as varchar(10))+' tinyint,'
set @i=@i+1
end
set @sql=@sql+'c'+cast(@i as varchar(10))+' tinyint)
--该表做笛卡尔积
declare @t table (p tinyint)
insert into @t
select 1
'
set @i=2
while @i<=@n
begin
set @sql=@sql+'union select '+cast(@i as varchar(10))+'
'
set @i=@i+1
end
set @sql=@sql+'
insert @c
select
*
from @t t1 '
set @i=2
while @i<=@n
begin
set @sql=@sql+'cross join @t t'+cast(@i as varchar(10))+' '
set @i=@i+1
end
set @sql=@sql+'
where 1=1
--竖
'
set @i=1
while @i<=@n-1
begin
set @j=@i+1
while @j<=@n
begin
set @sql=@sql+' and t'+cast(@i as varchar(10))+'.p<>t'+cast(@j as varchar(10))+'.p'
set @j=@j+1
end
set @sql=@sql+'
'
set @i=@i+1
end
/*
and t1.p<>t2.p and t1.p<>t3.p and t1.p<>t4.p and t1.p<>t5.p and t1.p<>t6.p and t1.p<>t7.p and t1.p<>t8.p
and t2.p<>t3.p and t2.p<>t4.p and t2.p<>t5.p and t2.p<>t6.p and t2.p<>t7.p and t2.p<>t8.p
and t3.p<>t4.p and t3.p<>t5.p and t3.p<>t6.p and t3.p<>t7.p and t3.p<>t8.p
and t4.p<>t5.p and t4.p<>t6.p and t4.p<>t7.p and t4.p<>t8.p
and t5.p<>t6.p and t5.p<>t7.p and t5.p<>t8.p
and t6.p<>t7.p and t6.p<>t8.p
and t7.p<>t8.p
*/
set @sql=@sql+'
--右斜
'
set @i=1
while @i<=@n-1
begin
set @j=@i+1
while @j<=@n
begin
set @sql=@sql+' and t'+cast(@i as varchar(10))+'.p<>t'+cast(@j as varchar(10))+'.p-'+cast(@j-@i as varchar(10))
set @j=@j+1
end
set @sql=@sql+'
'
set @i=@i+1
end
/*
and t1.p<>t2.p-1 and t1.p<>t3.p-2 and t1.p<>t4.p-3 and t1.p<>t5.p-4 and t1.p<>t6.p-5 and t1.p<>t7.p-6 and t1.p<>t8.p-7
and t2.p<>t3.p-1 and t2.p<>t4.p-2 and t2.p<>t5.p-3 and t2.p<>t6.p-4 and t2.p<>t7.p-5 and t2.p<>t8.p-6
and t3.p<>t4.p-1 and t3.p<>t5.p-2 and t3.p<>t6.p-3 and t3.p<>t7.p-4 and t3.p<>t8.p-5
and t4.p<>t5.p-1 and t4.p<>t6.p-2 and t4.p<>t7.p-3 and t4.p<>t8.p-4
and t5.p<>t6.p-1 and t5.p<>t7.p-2 and t5.p<>t8.p-3
and t6.p<>t7.p-1 and t6.p<>t8.p-2
and t7.p<>t8.p-1
*/
set @sql=@sql+'
--左斜
'
set @i=1
while @i<=@n-1
begin
set @j=@i+1
while @j<=@n
begin
set @sql=@sql+' and t'+cast(@i as varchar(10))+'.p<>t'+cast(@j as varchar(10))+'.p+'+cast(@j-@i as varchar(10))
set @j=@j+1
end
set @sql=@sql+'
'
set @i=@i+1
end
/*
and t1.p<>t2.p+1 and t1.p<>t3.p+2 and t1.p<>t4.p+3 and t1.p<>t5.p+4 and t1.p<>t6.p+5 and t1.p<>t7.p+6 and t1.p<>t8.p+7
and t2.p<>t3.p+1 and t2.p<>t4.p+2 and t2.p<>t5.p+3 and t2.p<>t6.p+4 and t2.p<>t7.p+5 and t2.p<>t8.p+6
and t3.p<>t4.p+1 and t3.p<>t5.p+2 and t3.p<>t6.p+3 and t3.p<>t7.p+4 and t3.p<>t8.p+5
and t4.p<>t5.p+1 and t4.p<>t6.p+2 and t4.p<>t7.p+3 and t4.p<>t8.p+4
and t5.p<>t6.p+1 and t5.p<>t7.p+2 and t5.p<>t8.p+3
and t6.p<>t7.p+1 and t6.p<>t8.p+2
and t7.p<>t8.p+1
*/
set @sql=@sql+'
declare @n int
set @n='+cast(@n as varchar(10))+'
select ''''
'
set @i=1
while @i<=@n
begin
set @sql=@sql+'+char(13)+REPLACE(space(c'+cast(@i as varchar(10))+'-1),'' '',''.'')+''#''+REPLACE(space(@n-c'+cast(@i as varchar(10))+'),'' '',''.'')
'
set @i=@i+1
end
set @sql=@sql+'+char(13)+REPLACE(space('+cast(@n as varchar(10))+'),'' '',''='')
from @c'
print len(@sql)
exec sp_executesql @sql
n==12:
2010-04-23 13:57:13.717
2010-04-23 13:57:13.717
5055
2010-04-23 13:57:13.717
(12 行受影响)
(14200 行受影响)
.#..........
......#.....
.........#..
...........#
........#...
....#.......
#...........
...#........
..........#.
.......#....
.....#......
..#.........
============
.#..........
...........#
........#...
......#.....
...#........
.........#..
#...........
....#.......
..........#.
.......#....
.....#......
..#.........
============
...
..........#.
#...........
...#........
.....#......
........#...
..#.........
...........#
.......#....
.#..........
....#.......
......#.....
.........#..
============
..........#.
.....#......
..#.........
#...........
...#........
.......#....
...........#
........#...
.#..........
....#.......
......#.....
.........#..
============
(14200 行受影响)
2010-04-23 13:57:33.193