Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 Prototype教程 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测
SQL Server Oracle Access MySQL
当前位置: 主页 > 数据库 > SQL Server >

SQL递归游戏

时间:2010-07-29 08:56来源:未知 作者:admin 点击:

5个flash的游戏地址是
http://www.sostart.com/article/view.php/765
此类游戏一般都是通过穷举或者递归之类的方法来求解,对于编成语言来说都比较简单。
这里用SQL语言的CTE递归来玩玩看。我的算法和答案如下:

 


 
/*
    四个字段分别代表 羊,狼,草,人的位置,0表示河左边,1表示河右边。
    每次都必须有人过河,因为动物和植物们都不会划船。
*/
declare @t table (y bit , l bit , c bit , r bit , path varchar (8000 ))
insert into @t select 0 , 0 , 0 , 0 , ''

;with t
as
(
    select * , 0 as cc from @t union all
    select ~ y , l , c , ~ r , path + '人羊' + ltrim (r )+ '→' , cc + 1 from t where cc < 10 and y = r and y & l & c = 0 and path not like '%人羊_→' union all
    select y , ~ l , c , ~ r , path + '人狼' + ltrim (r )+ '→' , cc + 1 from t where cc < 10 and   l = r and y & l & c = 0 and y <> c and path not like '%人狼_→' union all
    select y , l , ~ c , ~ r , path + '人草' + ltrim (r )+ '→' , cc + 1 from t where   cc < 10 and c = r and y & l & c = 0 and y <> l and path not like '%人草_→' union all
    select y , l , c , ~ r , path + '人' + ltrim (r )+ '→' , cc + 1 from t where cc < 10 and   y & l & c = 0 and y <> c and y <> l and path not like '%人_→'
   
)
select REPLACE (REPLACE (path , '0' , '过河' ), '1' , '返回' ) as path from t where y & l & c = 1

/*
人羊过河→人返回→人草过河→人羊返回→人狼过河→人返回→人羊过河→
人羊过河→人返回→人狼过河→人羊返回→人草过河→人返回→人羊过河→
*/

 


(责任编辑:admin)
Tags:递归 SQL游戏
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价