11,848
社区成员
发帖
与我相关
我的任务
分享
--第四关:跳马
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(pstart,p,c,x,y) as
(
select p,p,1,x,y from @t
union all
select t.pstart,t.p+'->'+t_next.p,c+1,t_next.x,t_next.y from @t t_next,t where
(
(abs(t_next.x-t.x)=1 and abs(t_next.y-t.y)=2) or
(abs(t_next.x-t.x)=2 and abs(t_next.y-t.y)=1) )
and (CHARINDEX(t_next.p,t.p)=0 or (c=14 and t_next.p=t.pstart))
)
select p from t where c=15