|
-- 5X5的棋盘上摆放5个皇后,使得它们攻击不到的安全点最多(各皇后之间可以互相攻击)。
-- 答案是最多有3个安全点
with tmp1 as(select rownum as p from dual connect by rownum <= 5),
tmp2 as(select a.p as x,b.p as y, (a.p-1)*5+b.p as t from tmp1 a, tmp1 b),
tmp3 as(
select
a.x as a_x, a.y as a_y,
b.x as b_x, b.y as b_y,
c.x as c_x, c.y as c_y,
d.x as d_x, d.y as d_y,
e.x as e_x, e.y as e_y
from tmp2 a, tmp2 b, tmp2 c, tmp2 d, tmp2 e
where
a.t < b.t and b.t < c.t and c.t < d.t and d.t < e.t)
--
select
'('||a_x||','||a_y||')' as P1,
'('||b_x||','||b_y||')' as P2,
'('||c_x||','||c_y||')' as P3,
'('||d_x||','||d_y||')' as P4,
'('||e_x||','||e_y||')' as P5,
ct
from
(select
b.a_x,b.b_x,b.c_x,b.d_x,b.e_x,b.a_y,b.b_y,b.c_y,b.d_y,b.e_y,
count(*) as ct,max(count(*))over() as max_ct
from tmp2 a, tmp3 b
where abs(a.x-b.a_x)<>abs(a.y-b.a_y) and abs(a.x-b.b_x)<>abs(a.y-b.b_y)
and abs(a.x-b.c_x)<>abs(a.y-b.c_y) and abs(a.x-b.d_x)<>abs(a.y-b.d_y)
and abs(a.x-b.e_x)<>abs(a.y-b.e_y)
and a.x not in (b.a_x,b.b_x,b.c_x,b.d_x,b.e_x)
and a.y not in (b.a_y,b.b_y,b.c_y,b.d_y,b.e_y)
group by b.a_x,b.b_x,b.c_x,b.d_x,b.e_x,b.a_y,b.b_y,b.c_y,b.d_y,b.e_y)
where ct = max_ct
-- Result
P1 P2 P3 P4 P5 CT
1 (1,2) (1,3) (2,2) (2,5) (3,5) 3
2 (1,3) (1,4) (2,1) (2,4) (3,1) 3
3 (1,2) (1,3) (3,1) (4,1) (4,2) 3
4 (1,3) (1,4) (3,5) (4,4) (4,5) 3
5 (2,1) (2,2) (3,1) (5,2) (5,3) 3
6 (2,4) (2,5) (3,5) (5,3) (5,4) 3
7 (3,1) (4,1) (4,4) (5,3) (5,4) 3
8 (3,5) (4,2) (4,5) (5,2) (5,3) 3
-- 8 rows selected in 0.266 seconds |
|