|
|
Last effort on 5-connected cells. Basically using collections, one is all already connected and one not connected (similar to Dijkstra algorithm implement). for any 5 digit string eg 12345 each step only picks one connected cell (if possible). So any string goes through to 5 steps must be valid one (go to next step for coloring then, skip here). The forcus is on find valid 5 connected cells for all combinations C(9,5).
--==================================
with t0 as (select rownum cell from dual connect by rownum<=9
), t (rn, cur_cell, str) as (
select 1, cell, ''||cell str from t0
union all
select rn+1, cell, str||cell from t, t0
where cur_cell < cell and rn<5
), tmp (rn, str, cur_str, cur_cell, sum_peer, pos) as (
select 1, str, substr(str, 2) cur_str, substr(str, 1, 1) cur_cell,
decode(mod(substr(str, 1, 1),3),0,'',substr(str, 1, 1)+1)||'|'||decode(ceil(substr(str, 1, 1)/3),3,'',substr(str, 1, 1)+3) sum_peer,
--replace above cur_str/sum_peer in regexp_instr(cur_str, sum_peer, 1, 1) as below
regexp_instr(substr(str, 2), decode(mod(substr(str, 1, 1),3),0,'',substr(str, 1, 1)+1)||'|'||decode(ceil(substr(str, 1, 1)/3),3,'',substr(str, 1, 1)+3), 1, 1) pos
from t
union all
select rn+1, str, replace(cur_str, substr(cur_str, pos, 1)) cur_str, substr(cur_str, pos, 1) cur_cell,
sum_peer||'|'||decode(mod(substr(cur_str, pos, 1),3),0,'',substr(cur_str, pos, 1)+1)||'|'||decode(mod(substr(cur_str, pos, 1),3),1,'',substr(cur_str, pos, 1)-1)||'|'||decode(ceil(substr(cur_str, pos, 1)/3),3,'',substr(cur_str, pos, 1)+3)||'|'||decode(ceil(substr(cur_str, pos, 1)/3),1,'',substr(cur_str, pos, 1)-3) sum_peer,
regexp_instr(replace(cur_str, substr(cur_str, pos, 1)), sum_peer||'|'||decode(mod(substr(cur_str, pos, 1),3),0,'',substr(cur_str, pos, 1)+1)||'|'||decode(mod(substr(cur_str, pos, 1),3),1,'',substr(cur_str, pos, 1)-1)||'|'||decode(ceil(substr(cur_str, pos, 1)/3),3,'',substr(cur_str, pos, 1)+3)||'|'||decode(ceil(substr(cur_str, pos, 1)/3),1,'',substr(cur_str, pos, 1)-3), 1, 1) pos
from tmp where rn>0 and pos>0
)
select count(*) from tmp
where rn=5
/
--====================================
find 66 valid 5 connected cells for all combinations C(9,5)=126
SQL> /
COUNT(*)
----------
66
Elapsed: 00:00:00.04
SQL>
SQL>
|
|