SQL递归游戏-你厉害吗,来过5关
以下是改写后的文本内容
/*
四个字段分别代表 羊,狼,草,人的位置,0表示河左边,1表示河右边。
每次都必须有人过河,因为动物和植物们都不会划船。
*/
declare @t table ( y bit , l bit , c bit , r bit , path varchar ( 8000 ))
insert into @t select 0 , 0 , 0 , 0 , ''
执行路径生成操作并追加到结果集字段cc中。
该方法通过多条子查询实现复杂的业务逻辑处理。
每条子查询均基于不同条件组合构建特定的执行路径。
其中:
- 第一条子查询负责处理cc值小于十的情况;
- 第二条子查询针对y等于r的情形;
- 第三条子查询适用于c等于r的情况;
- 最后一条子查询则用于一般性处理。
所有结果都会被整合到主表t中;
每条记录都会被赋予相应的执行标记;
整个过程均需满足特定的条件约束。
)
select REPLACE ( REPLACE ( path , '0' , '过河' ), '1' , '返回' ) as path from t where y & l & c = 1
人们与动物一起渡河←人们离开后… 人们与动物一起渡河←人们离开后…
在人们的引导下, 狼捕食者们离开. 在人们的引导下, 狼捕食者们离开.
随后, 羊群随后, 羊群
随后, 草儿随后, 草儿
随后, 狼捕食者们随后, 狼捕食者们
随后, 人们与动物一起渡河←人们离开后… 随后, 人们与动物一起渡河←人们离开后…
接着再次重复上述过程. 接着再次重复上述过程.
/*
前五个字段分别代表 左人数,左鬼数,右人数,右鬼数,船位置
船位置为代表在右边,代表在左边
当右边没有人和鬼时(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 )
插入到 @t 表中
插入 slack person 并赋值 1
插入 slim 女孩 并赋值 3
插入 petite 女士 并赋值 6
插入 compact 女士 并赋值 8
插入 short man 并赋值 12
; with t ( forword_name , time , path ) as (
select 将 a 的名称与 b 的名称合并后用 c 替换为空的值, 将 b 的时间与 c 的时间相加, 将 a 和 b 的名称连接后添加“过河→”,再连接 c 并添加“返回→”
from @t_a, @t_b, @t_c where a的时间小于b的时间,并且检查字符位置是否大于零
union all
选取路径、时间字段从表t中查询满足条件的数据,
其中根据条件设置路径字段:
当forword_name长度小于6时,
执行字符串替换操作:将forword_name、a表名称及b表名称组合后替换掉c表名称,
否则直接使用forword_name、a表名称及b表名称组合;
时间字段则根据forword_name长度决定计算方式:
当forword_name长度小于6时,
计算t表时间加上b表时间再加上c表时间,
否则仅计算t表时间加上b表时间;
路径字段则基于条件判断生成动态组合结果:
当forword_name长度小于6时,
构造完整路径并附加特定操作序列:过河前往、返回;否则仅构造基本路径。
所有查询限定条件包括:
a表时间小于b表时间,
以及c表名称在字符串组合中出现位置大于零,
同时确保a名与t对应字段无关联,
并且b名与t对应字段也无关联。
结果输出至临时存储空间@t a, @t b, @t c, t中。
/*以下是操作序列说明:
"瘦人"经过"小胖"过河后前往"大胖瘸子"再返回至"瘦人";
持续此流程若干次...
总共包含40次重复操作
*/
/*第四关:跳马
*/
declare @t table (p varchar ( max ), x int , y int )
insert into @t
select 'p11' , 1 , 1 union
select 'p12' , 1 , 2 union
select 'p13' , 1 , 3 union
select 'p14' , 1 , 4 union
select 'p21' , 2 , 1 union
select 'p22' , 2 , 2 union
select 'p23' , 2 , 3 union
select 'p24' , 2 , 4 union
select 'p31' , 3 , 1 union
select 'p32' , 3 , 2 union
select 'p33' , 3 , 3 union
select 'p34' , 3 , 4 union
select 'p42' , 4 , 2 union
select 'p43' , 4 , 3
with t (p_{start}, p, c, x, y) as (
select p, self_p, 1, x, y from \mathbb{T}
union all
select t.(p_{start}),
t\text{\_}p + "→" + t\text{\_}next\text{\_}p,
(c + 1),
t\text{\_}next\text{\_}x,
t\text{\_}next\text{\_}y
from \mathbb{T} as \mathbb{T}_{next} cross join \mathbb{T} as t
where
(
Math.Abs(t\text{\_}next\text{\_}x - t.x) = 1
AndAlso Math.Abs(t\text{\_}next.y - t.y) = 2
Or
(
(c = 14)
And CharIndex(t.next)\.$p$, $t$\._\(p)) > 0
)
)
)
select ($p from \mathbb{T}\) where ($c) = 15
从起始点p43出发依次经过一系列中间点包括p31 p23 p11等最终返回至终点p43
该路径网络由多条闭合回路构成每条回路均以不同顺序组合这些关键节点
各节点间的连接关系形成一个复杂的网络系统确保信息传递的多向性
整个网络架构通过循环连接实现了资源传输的闭环管理
该系统设计充分考虑了节点间的平衡分布保证了网络运行效率的最大化
; with t as (
select 17 as f1 , 26 as f2 , 20 as f3 , 19 as f4 , 31 as f5 , cast ( '' as varchar ( 8000 )) as path
union all select f1 + 8 , f2 + 8 , f3 , f4 , f5 , path + '12上→' from t where f1 < 21 and f2 < 21
union all select f1 + 8 , f2 , f3 + 8 , f4 , f5 , path + '13上→' from t where f1 < 21 and f3 < 21
union all select f1 + 8 , f2 , f3 , f4 + 8 , f5 , path + '14上→' from t where f1 < 21 and f4 < 21
union all select f1 + 8 , f2 , f3 , f4 , f5 + 8 , path + '15上→' from t where f1 < 21 and f5 < 21
union all select f1 , f2 + 8 , f3 + 8 , f4 , f5 , path + '23上→' from t where f2 < 21 and f2 < 21
union all select f1 , f2 + 8 , f3 , f4 + 8 , f5 , path + '24上→' from t where f2 < 21 and f4 < 21
union all select f1 , f2 + 8 , f3 , f4 , f5 + 8 , path + '25上→' from t where f2 < 21 and f5 < 21
union all select f1 , f2 , f3 + 8 , f4 + 8 , f5 , path + '34上→' from t where f3 < 21 and f4 < 21
union all select f1 , f2 , f3 + 8 , f4 , f5 + 8 , path + '35上→' from t where f3 < 21 and f5 < 21
union all select f1 , f2 , f3 , f4 + 8 , f5 + 8 , path + '45上→' from t where f4 < 21 and f5 < 21
union all select f1 - 13 , f2 - 13 , f3 , f4 , f5 , path + '12下→' from t where f1 > 25 and f2 > 25
union all select f1 - 13 , f2 , f3 - 13 , f4 , f5 , path + '13下→' from t where f1 > 25 and f3 > 25
union all select f1 - 13 , f2 , f3 , f4 - 13 , f5 , path + '14下→' from t where f1 > 25 and f4 > 25
union all select f1 - 13 , f2 , f3 , f4 , f5 - 13 , path + '15下→' from t where f1 > 25 and f5 > 25
union all select f1 , f2 - 13 , f3 - 13 , f4 , f5 , path + '23下→' from t where f2 > 25 and f2 > 25
union all select f1 , f2 - 13 , f3 , f4 - 13 , f5 , path + '24下→' from t where f2 > 25 and f4 > 25
union all select f1 , f2 - 13 , f3 , f4 , f5 - 13 , path + '25下→' from t where f2 > 25 and f5 > 25
union all select f1 , f2 , f3 - 13 , f4 - 13 , f5 , path + '34下→' from t where f3 > 25 and f4 > 25
union all select f1 , f2 , f3 - 13 , f4 , f5 - 13 , path + '35下→' from t where f3 > 25 and f5 > 25
union all select f1 , f2 , f3 , f4 - 13 , f5 - 13 , path + '45下→' from t where f4 > 25 and f5 > 25
union all select f1 + 8 , f2 + 8 , f3 , f4 , f5 , path + '12上→' from t where f1 < 21 and f2 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 + 8 , f2 , f3 + 8 , f4 , f5 , path + '13上→' from t where f1 < 21 and f3 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 + 8 , f2 , f3 , f4 + 8 , f5 , path + '14上→' from t where f1 < 21 and f4 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 + 8 , f2 , f3 , f4 , f5 + 8 , path + '15上→' from t where f1 < 21 and f5 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 , f2 + 8 , f3 + 8 , f4 , f5 , path + '23上→' from t where f2 < 21 and f2 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 , f2 + 8 , f3 , f4 + 8 , f5 , path + '24上→' from t where f2 < 21 and f4 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 , f2 + 8 , f3 , f4 , f5 + 8 , path + '25上→' from t where f2 < 21 and f5 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 , f2 , f3 + 8 , f4 + 8 , f5 , path + '34上→' from t where f3 < 21 and f4 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 , f2 , f3 + 8 , f4 , f5 + 8 , path + '35上→' from t where f3 < 21 and f5 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
union all select f1 , f2 , f3 , f4 + 8 , f5 + 8 , path + '45上→' from t where f4 < 21 and f5 < 23 and f3 between 21 and 25 and f4 between 21 and 25 and f5 between 21 and 25
)
从t表中选择路径路径名;
其中,
f₁位于区间[₂₁,₂₅]之间,
f₂位于区间[₂₁,₂₅]之间,
...
以此类推,
直到
f₅位于区间[₂₁,₂₅]之间。
依次执行以下操作:
- 34单元向上转动;
- 45单元向下转动;
- 45单元再次向上转动;
- 35单元向下转动;
...
按照同样的顺序循环执行以上操作: - 14单元向上转动;
- 24单元向下转动;
- 24单元再次向上转动;
如此反复操作直至完成整个流程
