|
/*
前五个字段分别代表 左人数,左鬼数,右人数,右鬼数,船位置 船位置为代表在右边,代表在左边 当右边没有人和鬼时(gr+pr>0),不执行返回操作,递归结束 */ ;with t (pl , gl , pr , gr , boat , path ) as ( select 0 , 0 , 3 , 3 , cast (0 as bit ), cast ('' as varchar (8000 )) union all select pl + 2 as pl , gl , pr - 2 as pr , gr , ~ boat , path + '2人过河→' from t where boat = 0 and pr >= 2 and (pr - 2 >= gr or pr = 2 ) union all select pl + 1 , gl + 1 , pr - 1 , gr - 1 , ~ boat , path + '1人鬼过河→' from t where boat = 0 and pr >= 1 and gr >= 1 and pl >= gl union all select pl , gl + 2 , pr , gr - 2 , ~ boat , path + '2鬼过河→' from t where boat = 0 and gr >= 2 and (pl - 2 >= gl or pl = 0 ) union all select pl - 1 , gl , pr + 1 , gr , ~ boat , path + '1人返回→' from t where boat = 1 and pl >= 1 and gr + pr > 0 and (pl - 1 >= gl or pl = 1 ) and pr + 1 >= gr union all select pl , gl - 1 , pr , gr + 1 , ~ boat , path + '1鬼返回→' from t where boat = 1 and gl >= 1 and gr + pr > 0 and (pr - 1 >= gr or pr = 0 ) union all select pl - 1 , gl - 1 , pr + 1 , gr + 1 , ~ boat , path + '1人鬼返回→' from t where boat = 1 and pl >= 1 and gl >= 1 and gr + pr > 0 and pr >= gr and path not like '%1人鬼过河→' ) select path from t where pr = 0 and gr = 0 /* 2鬼过河→鬼返回→鬼过河→鬼返回→人过河→人鬼返回→人过河→鬼返回→鬼过河→鬼返回→鬼过河→ 2鬼过河→鬼返回→鬼过河→鬼返回→人过河→人鬼返回→人过河→鬼返回→鬼过河→人返回→人鬼过河→ 1人鬼过河→人返回→鬼过河→鬼返回→人过河→人鬼返回→人过河→鬼返回→鬼过河→鬼返回→鬼过河→ 1人鬼过河→人返回→鬼过河→鬼返回→人过河→人鬼返回→人过河→鬼返回→鬼过河→人返回→人鬼过河→ */
![]() /*
第三关:一家人过河 结果:过河的全部组合有万多中情况,其中满足s之内的有种方法 关联的三个表a b c配合charindex函数分别可以表示出可以过河的两个人和可以返回的一个人 当没有人可以过河则递归自动结束 递归中的case语句可以表示当对岸有个人的时候就不需要返回 */ declare @t table (name varchar (8000 ), time int ) insert into @t select '瘦人' , 1 union select '小胖' , 3 union select '姑娘' , 6 union select '大胖' , 8 union select '瘸子' , 12 ;with t (forword_name , time , path ) as ( select replace (a . name + b. name , c . name , '' ), b. time + c . time , a . name + b. name + '过河→' + c . name + '返回→' from @t a , @t b, @t c where a . time < b. time and charindex (c . name , a . name + b. name )> 0 union all select case when len (forword_name )< 6 then replace (forword_name + a . name + b. name , c . name , '' ) else forword_name + a . name + b. name end , case when len (forword_name )< 6 then t . time + b. time + c . time else t . time + b. time end , case when len (forword_name )< 6 then path + a . name + b. name + '过河→' + c . name + '返回→' else path + a . name + b. name + '过河→' end from @t a , @t b, @t c , t where a . time < b. time and charindex (c . name , forword_name + a . name + b. name )> 0 and charindex (a . name , t . forword_name )= 0 and charindex (b. name , t . forword_name )= 0 ) select path , time from t where len (forword_name )= 10 and time <= 30 /* 瘦人小胖过河→小胖返回→大胖瘸子过河→瘦人返回→瘦人小胖过河→瘦人返回→瘦人姑娘过河→ 瘦人小胖过河→小胖返回→大胖瘸子过河→瘦人返回→瘦人小胖过河→瘦人返回→瘦人姑娘过河→ 瘦人小胖过河→小胖返回→大胖瘸子过河→瘦人返回→瘦人小胖过河→瘦人返回→瘦人姑娘过河→ 瘦人小胖过河→小胖返回→大胖瘸子过河→瘦人返回→瘦人小胖过河→瘦人返回→瘦人姑娘过河→ 瘦人小胖过河→小胖返回→大胖瘸子过河→瘦人返回→瘦人小胖过河→瘦人返回→瘦人姑娘过河→ 瘦人小胖过河→小胖返回→大胖瘸子过河→瘦人返回→瘦人姑娘过河→瘦人返回→瘦人小胖过河→ ...共40行 */ ![]() /*第四关:跳马 */ (责任编辑:admin) |







骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价