Quantcast
Channel: oracle – Oracle SQL
Viewing all articles
Browse latest Browse all 101

8 queens chess problem: solution in Oracle SQL

$
0
0

This is just another solution of this problem for a chessboard, but you can choose any size of the checkerboard:

with 
 t as (select level i, cast(level as varchar2(1)) c from dual connect by level<=&d)
,x(l,s,n) as (
       select 1 l, c s, chr(97)||c||' ' from t
       union all
       select l+1, x.s||t.c, n||chr(98+l)||i||' '
       from x
            join t
                 on instr(s,c)=0
                    and not exists(select 0 from dual 
                                   where L+1 - t.i = level - substr(s,level,1)
                                      or L+1 + t.i = level + substr(s,level,1)
                                   connect by level<=length(s))
       where L<&d
 )
select n
from x
where l=&d

8x8:
SQL> @tests/f
Size[8]: 8

N
--------------------------------------------------------------------------------
a1 c5 d8 e6 f3 g7 h2 i4
a1 c6 d8 e3 f7 g4 h2 i5
a1 c7 d4 e6 f8 g2 h5 i3
a1 c7 d5 e8 f2 g4 h6 i3
a2 c4 d6 e8 f3 g1 h7 i5
a2 c5 d7 e1 f3 g8 h6 i4
a2 c5 d7 e4 f1 g8 h6 i3
a2 c6 d1 e7 f4 g8 h3 i5
a2 c6 d8 e3 f1 g4 h7 i5
a2 c7 d3 e6 f8 g5 h1 i4
a2 c7 d5 e8 f1 g4 h6 i3
a2 c8 d6 e1 f3 g5 h7 i4
a3 c1 d7 e5 f8 g2 h4 i6
a3 c5 d2 e8 f1 g7 h4 i6
a3 c5 d2 e8 f6 g4 h7 i1
a3 c5 d7 e1 f4 g2 h8 i6
a3 c5 d8 e4 f1 g7 h2 i6
a3 c6 d2 e5 f8 g1 h7 i4
a3 c6 d2 e7 f1 g4 h8 i5
a3 c6 d2 e7 f5 g1 h8 i4
a3 c6 d4 e1 f8 g5 h7 i2
a3 c6 d4 e2 f8 g5 h7 i1
a3 c6 d8 e1 f4 g7 h5 i2
a3 c6 d8 e1 f5 g7 h2 i4
a3 c6 d8 e2 f4 g1 h7 i5
a3 c7 d2 e8 f5 g1 h4 i6
a3 c7 d2 e8 f6 g4 h1 i5
a3 c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 i7
a6 c3 d5 e7 f1 g4 h2 i8
a6 c3 d5 e8 f1 g4 h2 i7
a6 c3 d7 e2 f4 g8 h1 i5
a6 c3 d7 e2 f8 g5 h1 i4
a6 c3 d7 e4 f1 g8 h2 i5
a6 c4 d1 e5 f8 g2 h7 i3
a6 c4 d2 e8 f5 g7 h1 i3
a6 c4 d7 e1 f3 g5 h2 i8
a6 c4 d7 e1 f8 g2 h5 i3
a6 c8 d2 e4 f1 g7 h5 i3
a7 c1 d3 e8 f6 g4 h2 i5
a7 c2 d4 e1 f8 g5 h3 i6
a7 c2 d6 e3 f1 g4 h8 i5
a7 c3 d1 e6 f8 g5 h2 i4
a7 c3 d8 e2 f5 g1 h6 i4
a7 c4 d2 e5 f8 g1 h3 i6
a7 c4 d2 e8 f6 g1 h3 i5
a7 c5 d3 e1 f6 g8 h2 i4
a8 c2 d4 e1 f7 g5 h3 i6
a8 c2 d5 e3 f1 g7 h4 i6
a8 c3 d1 e6 f2 g5 h7 i4
a8 c4 d1 e3 f6 g2 h7 i5

92 rows selected.

[collapse]

It works quite fast:
8*8 ~ 0.1s
9*9 ~ 0.6s
10*10 ~4s
script for sqlplus
set arrays 1000;
col n for a80;
accept d prompt "Size[8]: " default 8;
with 
 t as (select/*+inline*/ level i, cast(level as varchar2(2)) c from dual connect by level<=&d)
,x(l,s,n) as (
       select 1 l, c s, chr(97)||c||' ' from t
       union all
       select l+1, x.s||t.c, n||chr(98+l)||i||' '
       from x
            join t
                 on instr(s,c)=0
                    and not exists(select 0 from dual 
                                   where L+1 - t.i = level - substr(s,level,1)
                                      or L+1 + t.i = level + substr(s,level,1)
                                   connect by level<=length(s))
       where L<&d
 )
select n
from x
where l=&d
/
col n clear;

[collapse]

Viewing all articles
Browse latest Browse all 101

Trending Articles