怀疑使用TSQL创建Sierpinski地毯(基于设置)

时间:2011-06-10 09:53:56

标签: sql-server-2005 tsql

TSql Challenge 56 结束了......所以我可以安全地怀疑那个

我试图在

下解决问题
WITH CTE AS 
( 
    SELECT   
            id ,
                        level ,
                        1 AS row ,
                        REPLICATE('X', POWER(3, level)) AS carpet
               FROM     TC56
               UNION ALL
               SELECT   id ,
                        level ,
                        row + 1 ,
                        carpet
               FROM     CTE
               WHERE    Row < POWER(3, level)
             )
    SELECT  
        id
        ,row
        ,carpet
    FROM    CTE
    ORDER BY id,row

但是输出与指定的输出不相符。我的输出在

之下
id row carpet
1 1 X
2 1 XXX
2 2 XXX
2 3 XXX
3 1 XXXXXXXXX
3 2 XXXXXXXXX
3 3 XXXXXXXXX
3 4 XXXXXXXXX
3 5 XXXXXXXXX
3 6 XXXXXXXXX
3 7 XXXXXXXXX
3 8 XXXXXXXXX
3 9 XXXXXXXXX

问题在于我无法填补需要填充空格的位置

需要帮助

1 个答案:

答案 0 :(得分:0)

这是一个可以扩展的解决方案,可以生成更大订单的Sierpinski地毯。目前它可以正确生成Sierpinski地毯,最高可达3 rd

通过'正确'我的意思是,可以为更大的订单模式生成所有必需的行,但是目前需要添加其他列(那些sortN个)以提供正确的行的排列。我已经添加了需要修改的注释。

WITH TC56 (id, level) AS (
  SELECT 1, 0 UNION ALL
  SELECT 2, 1 UNION ALL
  SELECT 3, 2
),
x AS (
  SELECT CAST('X' AS varchar(max)) AS x
),
rec_cte AS (
  SELECT
    x,
    level = 0,
    sort1 = 0,
    sort2 = 0,
    sort3 = 0
    /* add new columns here */
  FROM x
  UNION ALL
  SELECT
    x = CAST(x.x + CASE TC56.level WHEN 1 THEN SPACE(LEN(x.x)) ELSE x.x END + x.x
             AS varchar(max)),
    level = x.level + 1,
    sort1 = TC56.level,
    sort2 = (TC56.level * 3 + x.sort1) % 3,
    sort3 = ((TC56.level * 3 + x.sort1) * 3 + x.sort2) % 3
    /* computations of the new columns' values follow the above pattern; e.g.
    sort4 = (((TC56.level * 3 + x.sort1) * 3 + x.sort2) * 3 + x.sort3) % 3 */
  FROM rec_cte x
    CROSS JOIN TC56
  WHERE x.level < 3  /* the necessary level to generate;
                   the last sortN's N number defines the maximum value
                   for which the pattern produced is guaranteed to be correct;
                   in this script the last sort column is sort3, so
                   the maximum 'supported' order is 3 */
)
SELECT * FROM rec_cte
ORDER BY level, sort1, sort2, sort3 /* add the new columns here */

结果如下:

x                              level       sort1       sort2       sort3
------------------------------ ----------- ----------- ----------- -----------
X                              0           0           0           0
XXX                            1           0           0           0
X X                            1           1           0           0
XXX                            1           2           0           0
XXXXXXXXX                      2           0           0           0
X XX XX X                      2           0           1           0
XXXXXXXXX                      2           0           2           0
XXX   XXX                      2           1           0           0
X X   X X                      2           1           1           0
XXX   XXX                      2           1           2           0
XXXXXXXXX                      2           2           0           0
X XX XX X                      2           2           1           0
XXXXXXXXX                      2           2           2           0
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           0           0
X XX XX XX XX XX XX XX XX X    3           0           0           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           0           2
XXX   XXXXXX   XXXXXX   XXX    3           0           1           0
X X   X XX X   X XX X   X X    3           0           1           1
XXX   XXXXXX   XXXXXX   XXX    3           0           1           2
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           2           0
X XX XX XX XX XX XX XX XX X    3           0           2           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           2           2
XXXXXXXXX         XXXXXXXXX    3           1           0           0
X XX XX X         X XX XX X    3           1           0           1
XXXXXXXXX         XXXXXXXXX    3           1           0           2
XXX   XXX         XXX   XXX    3           1           1           0
X X   X X         X X   X X    3           1           1           1
XXX   XXX         XXX   XXX    3           1           1           2
XXXXXXXXX         XXXXXXXXX    3           1           2           0
X XX XX X         X XX XX X    3           1           2           1
XXXXXXXXX         XXXXXXXXX    3           1           2           2
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           0           0
X XX XX XX XX XX XX XX XX X    3           2           0           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           0           2
XXX   XXXXXX   XXXXXX   XXX    3           2           1           0
X X   X XX X   X XX X   X X    3           2           1           1
XXX   XXXXXX   XXXXXX   XXX    3           2           1           2
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           2           0
X XX XX XX XX XX XX XX XX X    3           2           2           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           2           2