楼主: newkid

[每日一题] PUZZLEUP 2017

[复制链接]
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
21#
发表于 2017-8-17 11:17 | 只看该作者
为 2*2 的情形:

SQL>
SQL>   with t as (select 'R' color from dual
  2                union all
  3               select 'Y' from dual
  4                union all
  5               select 'B' from dual),
  6         s as (select level n,
  7                      ceil(level/2) x,
  8                      decode(mod(level,2),0,2,mod(level,2)) y
  9                 from dual
10              connect by level <= 2*2),
11  r(len,str,x,y) as (select 1,color,1,1
12                       from t
13                      union all
14                     select r.len + 1,
15                            r.str||t.color,
16                            s.x,
17                            s.y
18                       from r,t,s
19                      where r.len < 4
20                        and r.len + 1 = s.n
21                        and t.color not in (select substr(r.str,s2.n,1) node
22                                              from s s2
23                                             where abs(s.x - s2.x) + abs(s.y - s2.y) = 1
24                                               and s2.n < r.len + 1
25                                               )
26                    )
27  select count(str)
28    from r
29   where len = 4
30     and instr(r.str,'R') > 0
31     and instr(r.str,'Y') > 0
32     and instr(r.str,'B') > 0
33  /
COUNT(STR)
----------
        12

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
22#
发表于 2017-8-17 11:19 | 只看该作者
newkid 发表于 2017-8-16 22:05
#2 PAINTED GRID

Using red, green and blue colors, paint every cell of a 4x4 grid such that every  ...

随便给一个都可以啊 。。。

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
23#
发表于 2017-8-17 11:19 | 只看该作者
〇〇 发表于 2017-8-17 08:03
共享一条边的两个单元格 就是 where abs(r1-r2)+abs(c1-c2)=1

借用的你的判断

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
24#
发表于 2017-8-17 11:51 | 只看该作者
solomon_007 发表于 2017-8-17 11:17
第二题:

SQL>   with t as (select 'R' color from dual

nice  

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
25#
发表于 2017-8-17 12:00 | 只看该作者

能否利用颜色的对称减少列举?
比如
rbbr和brrb同类

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
26#
发表于 2017-8-17 12:53 | 只看该作者

其实就是x-1r,c-1)and c>1

x-nr-1,c)

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
27#
发表于 2017-8-17 13:28 | 只看该作者
〇〇 发表于 2017-8-17 12:00
能否利用颜色的对称减少列举?
比如
rbbr和brrb同类

因为颜色只有3种,所以3顺序个可以相互替换,所以只计算一个颜色开头的,总数再乘以3就是结果:

  SQL> with t as (select 'R' color from dual
  2                union all
  3               select 'Y' from dual
  4                union all
  5               select 'B' from dual),
  6         s as (select level n,
  7                      ceil(level/4) x,
  8                      decode(mod(level,4),0,4,mod(level,4)) y
  9                 from dual
10              connect by level <= 4*4),
11  r(len,str,x,y) as (select 1,color,1,1
12                       from t
13                      union all
14                     select r.len + 1,
15                            r.str||t.color,
16                            s.x,
17                            s.y
18                       from r,t,s
19                      where r.len < 4*4
20                        and r.len + 1 = s.n
21                        and t.color not in (select substr(r.str,s2.n,1) node
22                                              from s s2
23                                             where abs(s.x - s2.x) + abs(s.y - s2.y) = 1
24                                               and s2.n < r.len + 1
25                                               )
26                        and substr(r.str,1,1) = 'B'   --随便取一个颜色
27                    )
28  select count(str)*3
29    from r
30   where len = 4*4
31     and instr(r.str,'R') > 0
32     and instr(r.str,'Y') > 0
33     and instr(r.str,'B') > 0
34  /
COUNT(STR)*3
------------
        7806

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
28#
发表于 2017-8-17 13:46 | 只看该作者
〇〇 发表于 2017-8-17 12:53
其实就是x-1r,c-1)and c>1

x-nr-1,c)

对,只判断正左边和正上边的格子就可以了:

SQL>
SQL>   with t as (select 'R' color from dual
  2                union all
  3               select 'Y' from dual
  4                union all
  5               select 'B' from dual),
  6         s as (select level n,
  7                      ceil(level/4) x,
  8                      decode(mod(level,4),0,4,mod(level,4)) y
  9                 from dual
10              connect by level <= 4*4),
11  r(len,str,x,y) as (select 1,color,1,1
12                       from t
13                      union all
14                     select r.len + 1,
15                            r.str||t.color,
16                            s.x,
17                            s.y
18                       from r,t,s
19                      where r.len < 4*4
20                        and r.len + 1 = s.n
21                        and t.color not in (select substr(r.str,s2.n,1) node
22                                              from s s2
23                                            -- where abs(s.x - s2.x) + abs(s.y - s2.y) = 1
24                                            where ((s.x - s2.x = 1 and s2.y = s.y ) or (s.y - s2.y = 1 and s.x = s2.x))
25                                               and s2.n < r.len + 1
26                                               )
27                        and substr(r.str,1,1) = 'B'
28                    )
29  select count(str)*3
30    from r
31   where len = 4*4
32     and instr(r.str,'R') > 0
33     and instr(r.str,'Y') > 0
34     and instr(r.str,'B') > 0
35  /
COUNT(STR)*3
------------
        7806

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
29#
发表于 2017-8-17 13:56 | 只看该作者
solomon_007 发表于 2017-8-17 13:46
对,只判断正左边和正上边的格子就可以了:

SQL>

速度快了不少

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
30#
发表于 2017-8-17 14:04 | 只看该作者

的确,多谢指点!!

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表