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 分页 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递归游戏(2)

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

    前五个字段分别代表 左人数,左鬼数,右人数,右鬼数,船位置
    船位置为代表在右边,代表在左边
    当右边没有人和鬼时(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 = 0union 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)
Tags:递归 SQL游戏
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价
  • 数据库中自定义拆分字符串函数Split()

    经常我们要用到批量操作时都会用到字符串的拆分,郁闷的是SQL Server中却没有自带Split函数,所以我们只能自己动手来解决一下。为了减少和数据库的通讯次数...

  • sql函数实现三种父子递归

    在实际运用中经常会创建这样的结构表Category(Id, ParentId, Name),特别是用于树形结构时(菜单树,权限树..),这种表设计自然而然地会用...

  • SQL CASE WHEN使用

    Case具有两种格式。简单Case函数和Case搜索函数。 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THE...

  • MSSQL移除字符串两边的指定字符

    移除字符串左边的字符: CREATE FUNCTION [ dbo ] . [ RemoveLeftChar ] ( @Expression varchar (...

  • IDENTITY的小技巧--SQL Server 2005

    备份数据库遇到一个问题,就是将旧数据搬移到新主机数据表的时候,如果主键是的 IDENTITY 是设定自动增加的话,那么旧有的主键数据,在新数据表上面就好像是重新...

  • Sql2005将相同值的行内容进行合并

    如何将相同键值的蓝位内容值串接 ? 举例来说 TableA 如下: ID Type DESC 1 cpu 处理器 1 cpu 双核心 1 cpu 800外频 2...

  • SQL Server数据库恢复案例分享

    很多数据恢复工程师包括一些数据恢复技术爱好者经常会问同样一个问题:数据一旦被覆盖了,还能不能恢复呀?我听说国外能恢复被覆盖以后的数据,据说只要是覆盖操作在7次以...

  • SQL2005索引优化

    SQL2005索引优化...

  • 容易忽略的SQL语句

    容易忽略的SQL语句...

  • 世界完全对称日(SQL)

    世界完全对称日(SQL)...

  • 经典的sql语句技巧

    1、应用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数;通过 搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担;能够分 开的操作尽量...

  • provider:SQL网络接口, error: 26-定位指定的服务器/实例时出错

    在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (prov...

  • 树形数据汇总查询(MS-SQL Server应用实例)

    -- SQL2000: -- 查询的数据语句: SELECT DISTINCT B.KJND,B.GSDM,A.FZDM,A.FZMC, ZBZE1 = SUM...

  • SQLServer批量备份与还原

    备份与还原是数据库避不开的主题,而作为DBA,经常会面临将一台机器上的所有数据库重新构建到一台新机器上的要求; 在现在都讲究自动化管理的时代,传统的界面操作备份...

  • 如何修改SQL Server 2005服务器名称

    1、使用SELECT @@ServerName可以看到当前数据库的服务器名 2、SELECT * FROM Sys.SysServers表中可以看到当前的所有服...