|
#2
--3*3
SQL>
SQL> with t(n,r,c) as (select level,ceil(level/3),decode(mod(level,3),0,3,mod(level,3))
2 from dual
3 connect by level <= 3*3),
4 L(lvl,nlist,pre_r,pre_c,last_r,last_c,nsum,c_cnt,r_cnt,turn_cnt) as (select 1,
5 cast(n as varchar2(1000)),
6 null,
7 null,
8 t.r,
9 t.c,
10 power(2,t.n),
11 1,
12 1,
13 0
14 from t
15 union all
16 select L.lvl + 1,
17 L.nlist||','||t.n,
18 L.last_r,
19 L.last_c,
20 t.r,
21 t.c,
22 L.nsum + power(2,t.n),
23 case when abs(L.last_c - t.c) = 1 then L.c_cnt + 1 else L.c_cnt end,
24 case when abs(L.last_r - t.r) = 1 then L.r_cnt + 1 else L.r_cnt end,
25 case when L.pre_c <> t.c and L.pre_r <> t.r then L.turn_cnt + 1 else L.turn_cnt end
26 from L,t
27 where abs(L.last_r - t.r) + abs(L.last_c - t.c) = 1
28 and bitand(L.nsum,power(2,t.n)) = 0
29 and case when L.turn_cnt = 0 then 1
30 when L.turn_cnt = 1 and (L.pre_c = t.c or L.pre_r = t.r) then 1
31 else 0
32 end = 1
33 and L.turn_cnt <=1 )
34 select count(distinct nsum) from L where turn_cnt = 1 and c_cnt <> r_cnt
35 /
COUNT(DISTINCTNSUM)
-------------------
16
--8*8
SQL>
SQL> with t(n,r,c) as (select level,ceil(level/8),decode(mod(level,8),0,8,mod(level,8))
2 from dual
3 connect by level <= 8*8),
4 L(lvl,nlist,pre_r,pre_c,last_r,last_c,nsum,c_cnt,r_cnt,turn_cnt) as (select 1,
5 cast(n as varchar2(1000)),
6 null,
7 null,
8 t.r,
9 t.c,
10 power(2,t.n),
11 1,
12 1,
13 0
14 from t
15 union all
16 select L.lvl + 1,
17 L.nlist||','||t.n,
18 L.last_r,
19 L.last_c,
20 t.r,
21 t.c,
22 L.nsum + power(2,t.n),
23 case when abs(L.last_c - t.c) = 1 then L.c_cnt + 1 else L.c_cnt end,
24 case when abs(L.last_r - t.r) = 1 then L.r_cnt + 1 else L.r_cnt end,
25 case when L.pre_c <> t.c and L.pre_r <> t.r then L.turn_cnt + 1 else L.turn_cnt end
26 from L,t
27 where abs(L.last_r - t.r) + abs(L.last_c - t.c) = 1
28 and bitand(L.nsum,power(2,t.n)) = 0
29 and case when L.turn_cnt = 0 then 1
30 when L.turn_cnt = 1 and (L.pre_c = t.c or L.pre_r = t.r) then 1
31 else 0
32 end = 1
33 and L.turn_cnt <=1 )
34 select count(distinct nsum) from L where turn_cnt = 1 and c_cnt <> r_cnt
35 /
COUNT(DISTINCTNSUM)
-------------------
2576
老办法! |
|