|
|
--6*6 + 2
SQL> set timing on
SQL>
SQL> with t(n,c,x,y) as (select level,chr(64+level),ceil(level/6),mod(level-1,6)+1 from dual connect by level<=6*6),
2 s(lvl,n,
3 --nlist,
4 clist) as (select 1,
5 n,
6 --cast(n as varchar2(100)),
7 c
8 from t
9 where t.n between 1 and 3
10 union all
11 select lvl + 1,
12 b.n,
13 --nlist||','||b.n,
14 clist||','||b.c
15 from s,t b
16 where s.n < b.n
17 and (select greatest(sum(case when a.x = b.x then 1 end),
18 sum(case when a.y = b.y then 1 end),
19 sum(case when a.x-b.x = a.y-b.y then 1 end),
20 sum(case when a.x-b.x = b.y-a.y then 1 end)
21 )
22 from t a
23 where instr(clist||','||b.c,a.c)>0 ) <=2
24 and case when lvl+1=2 and b.n between 1 and 6 then 1
25 when lvl+1 in (3,4) and b.n between 7 and 12 then 1
26 when lvl+1 in (5,6) and b.n between 13 and 18 then 1
27 when lvl+1 in (7,8) and b.n between 19 and 24 then 1
28 when lvl+1 in (9,10) and b.n between 25 and 30 then 1
29 when lvl+1 in (11,12) and b.n between 31 and 36 then 1
30 else 0
31 end = 1
32 )
33 select max(lvl) from s
34 /
MAX(LVL)
----------
12
Executed in 1.829 seconds
--7*7 + 2
SQL> set timing on;
SQL>
SQL> with t(n,c,x,y) as (select level,chr(64+level),ceil(level/7),mod(level-1,7)+1 from dual connect by level<=7*7),
2 s(lvl,n,
3 --nlist,
4 clist) as (select 1,
5 n,
6 --cast(n as varchar2(100)),
7 c
8 from t
9 where t.n between 1 and 4
10 union all
11 select lvl + 1,
12 b.n,
13 --nlist||','||b.n,
14 clist||','||b.c
15 from s,t b
16 where s.n < b.n
17 and (select greatest(sum(case when a.x = b.x then 1 end),
18 sum(case when a.y = b.y then 1 end),
19 sum(case when a.x-b.x = a.y-b.y then 1 end),
20 sum(case when a.x-b.x = b.y-a.y then 1 end)
21 )
22 from t a
23 where instr(clist||','||b.c,a.c)>0 ) <=2
24 and case when lvl+1=2 and b.n between 1 and 7 then 1
25 when lvl+1 in (3,4) and b.n between 8 and 14 then 1
26 when lvl+1 in (5,6) and b.n between 15 and 21 then 1
27 when lvl+1 in (7,8) and b.n between 22 and 28 then 1
28 when lvl+1 in (9,10) and b.n between 29 and 35 then 1
29 when lvl+1 in (11,12) and b.n between 36 and 42 then 1
30 when lvl+1 in (13,14) and b.n between 43 and 49 then 1
31 else 0
32 end = 1
33 )
34 select max(lvl) from s
35 /
MAX(LVL)
----------
14
Executed in 59.74 seconds
|
|