Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
WITH CTE AS (
SELECT 1 as n
UNION ALL
SELECT n+1
FROM CTE
WHERE n < 8
)
SELECT A.n as 'A', B.n as 'B', C.n as 'C', D.n as 'D', E.n as 'E', F.n as 'F', G.n as 'G', H.n as 'H'
FROM CTE AS A
CROSS JOIN CTE AS B
CROSS JOIN CTE AS C
CROSS JOIN CTE AS D
CROSS JOIN CTE AS E
CROSS JOIN CTE AS F
CROSS JOIN CTE AS G
CROSS JOIN CTE AS H
WHERE A.n NOT IN (B.n,C.n,D.n,E.n,F.n,G.n,H.n,B.n+1,C.n+2,D.n+3,E.n+4,F.n+5,G.n+6,H.n+7,B.n-1,C.n-2,D.n-3,E.n-4,F.n-5,G.n-6,H.n-7)
AND B.n NOT IN (C.n,D.n,E.n,F.n,G.n,H.n,C.n+1,D.n+2,E.n+3,F.n+4,G.n+5,H.n+6,C.n-1,D.n-2,E.n-3,F.n-4,G.n-5,H.n-6)
AND C.n NOT IN (D.n,E.n,F.n,G.n,H.n,D.n+1,E.n+2,F.n+3,G.n+4,H.n+5,D.n-1,E.n-2,F.n-3,G.n-4,H.n-5)
AND D.n NOT IN (E.n,F.n,G.n,H.n,E.n+1,F.n+2,G.n+3,H.n+4,E.n-1,F.n-2,G.n-3,H.n-4)
AND E.n NOT IN (F.n,G.n,H.n,F.n+1,G.n+2,H.n+3,F.n-1,G.n-2,H.n-3)
AND F.n NOT IN (G.n,H.n,G.n+1,H.n+2,G.n-1,H.n-2)
AND G.n NOT IN (H.n,H.n+1,H.n-1)
ORDER BY A.n,B.n,C.n,D.n,E.n,F.n,G.n
with
x(i) as (select level from dual connect by level <= 8),
q(z, w) as (select '0', '' from dual
union all
select z || i, w || substr('abcdefgh', length(z), 1) || i || ' ' from q cross join x
where length(z) <= 8
and 0 = (select count(*) from x y
where length(z) > y.i
and (to_number(substr(z, y.i + 1, 1)) - x.i) / (length(z) - y.i) IN (-1, 0, 1)))
cycle z set cyclemark to 'X' default '-'
select w from q where length(z) - 1 = 8
.with
x(i) as (select level from dual connect by level <= 8),
q(z) as (select '.' from dual
union all
select z || i from q inner join x
on length(z) < 8)
select * from q
where length(z) - 1 = 8
not 1 in (select 1 from x y
where length(z) > y.i
and (x.i - substr(z, y.i + 1, 1)) / (length(z) - y.i) IN (-1, 0, 1))
with
x(i) as (select level from dual connect by level <= 8),
q(z) as (select '.' from dual
union all
select z || i from q inner join x
on not 1 in (select 1 from x y
where length(z) > y.i
and (x.i - substr(z, y.i + 1, 1)) / (length(z) - y.i) IN (-1, 0, 1)))
select * from q where length(z) - 1 = 8
with
x(i) as (select level from dual connect by level <= 8),
q(z, w) as (select '.', '' from dual
union all
select z || i, w || substr('abcdefgh', length(z), 1) || i || ' ' from q inner join x
on not 1 in (select 1 from x y
where length(z) > y.i
and (x.i - substr(z, y.i + 1, 1)) / (length(z) - y.i) IN (-1, 0, 1)))
cycle z set cyclemark to 'X' default '-'
select w from q where length(z) - 1 = 8
cycle z set cyclemark to 'X' default '-'
with
x(i) as (select level from dual connect by level <= 8),
q(z, j) as (select '', 1 from dual
union all
select z || y.i, j + 1 from q inner join x y
on not exists(select 1 from x where i < j and (y.i - substr(z, i, 1)) / (j - i) IN (-1, 0, 1)))
cycle z set cyclemark to 'X' default '-'
select translate('a1 b2 c3 d4 e5 f6 h7 g8', '12345678', z) from q where j = 8 + 1
Задача о восьми Ферзях на Oracle SQL (другое решение)