|
上面的稍稍改进一下, 就是一旦走到右边和上边,就没有选择了,就可以终止往下走了。 增加一个stop列, 表示终止递归。
with targ as (select 8+1 arg from dual)
,tree as (select rownum x from dual connect by rownum<=2)
, t (step, r, c,path, stop) as
(select 0 step, 1 r, 1 c, '', 0 from dual
union all
select
step+1,
case when (x=2 and r< arg) or c=arg then r+1 else r end,
case when (x=1 and c< arg) or r=arg then c+1 else c end,
path || '|' || r || ',' || c ,
case when r=arg or c= arg then 1 else 0 end
from t, tree,targ where step<arg*2-1 and stop = 0)
cycle step,r,c set iscycle to 'y' default 'n'
select count(distinct path) from t,targ where stop = 1
COUNT(DISTINCTPATH)
-------------------
12870
Executed in 2.61 seconds |
|