|
#14
--这里先给出棋盘长度为6的情况:
--我的解法:-----------------------------------------------------------------------------------------------------
create or replace function fn_one_step(p_str in varchar2) --这个是节点走一步,输出所有的邻居节点
return t_str pipelined
is
j int:=0;
l_cnt int:=0;
begin
for i in 1..length(p_str) loop
if substr(p_str,i,1)=1 and i<length(p_str) then
if substr(p_str,i+1,1) = 0 then
pipe row(substr(p_str,1,i-1)||'01'||substr(p_str,i+2));
else
if substr(p_str,i+2,1) = 0 then
--从第i+1开始,判断后面连续出现的'10'的个数
j:=i+1;
loop
exit when j>length(p_str);
if substr(p_str,j,2)='10' then
l_cnt := l_cnt +1;
else
exit;
end if;
j:=j+2;
end loop;
pipe row(substr(p_str,1,i-1)||'0'
||replace(rpad('$',(l_cnt-1)*2+1,'10'),'$')
||'11'
||substr(p_str,i+l_cnt*2+1));
end if;
end if;
end if;
end loop;
return;
end;
with t as (select level-1 n from dual connect by level <=2),
s(lvl,str) as ( select 1 lvl,
cast(n as varchar2(6)) str
from t
union all
select s.lvl+1,
s.str||t.n
from s,t
where lvl<6),
r as ( select str
from s
where lvl=6
and regexp_count(str,'1',1)=3),
p as (
select r.str,
sub.column_value as sub_str
from r,table(fn_one_step(r.str)) sub)
select substr(sys_connect_by_path(str,',')||','||sub_str,2) as res
from p
where sub_str='000111'
and level = (select min(level)
from p
where sub_str='000111'
start with str='111000'
connect by prior sub_str = str)
and rownum =1
start with str='111000'
connect by prior sub_str = str;
SQL> with t as (select level-1 n from dual connect by level <=2),
2 s(lvl,str) as ( select 1 lvl,
3 cast(n as varchar2(6)) str
4 from t
5 union all
6 select s.lvl+1,
7 s.str||t.n
8 from s,t
9 where lvl<6),
10 r as ( select str
11 from s
12 where lvl=6
13 and regexp_count(str,'1',1)=3),
14 p as (
15 select r.str,
16 sub.column_value as sub_str
17 from r,table(fn_one_step(r.str)) sub)
18 select substr(sys_connect_by_path(str,',')||','||sub_str,2) as res
19 from p
20 where sub_str='000111'
21 and level = (select min(level)
22 from p
23 where sub_str='000111'
24 start with str='111000'
25 connect by prior sub_str = str)
26 and rownum =1
27 start with str='111000'
28 connect by prior sub_str = str;
RES
--------------------------------------------------------------------------------
111000,101100,011100,011010,001011,000111
|
|