楼主: newkid

[精华] 出个SQL题:4皇后问题(新增马步问题在第7页)

[复制链接]
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
41#
 楼主| 发表于 2010-8-18 23:21 | 只看该作者
6666444的做法其实和滚珠是很类似的,只是用标量子查询代替了表连接。t2里面那些trunc, mod都可以预先在t中算好。a.l*1e6+b.l*1e4+c.l*1e2+d.l可以改用带分隔符的字符串合并,然后后面用instr判断。
我的两个答案其实是旋转对称的。

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
42#
 楼主| 发表于 2010-8-19 01:31 | 只看该作者
把我最后的四表连接改用11GR2的递归WITH实现,consistent gets从3360下降到22, 虽然执行时间没有什么差别。
WITH c1 AS (
   SELECT ROWNUM id, MOD(ROWNUM-1,7)+1 x,CEIL(ROWNUM/7) y FROM DUAL CONNECT BY ROWNUM<=49
   )
  ,cells AS (
   SELECT c1.id,c1.x,c1.y
         ,REPLACE(SYS_CONNECT_BY_PATH(CASE WHEN c1.x=c2.x OR c1.y = c2.y
                                                OR c1.x-c2.x IN (c1.y-c2.y,c2.y-c1.y)
                                           THEN '1'
                                           ELSE '0'
                                      END,',')
                 ,',') cover
     FROM c1, c1 c2
    WHERE level=49
    START WITH c2.id=1
   CONNECT BY c1.id = PRIOR c1.id AND c2.id = PRIOR c2.id+1
   )
  ,t(id,cover,result,lvl) AS (
   SELECT id,cover,'('||x||','||y||')' AS result,1
     FROM cells
    WHERE x BETWEEN 1 AND 4
          AND y BETWEEN 1 AND 4
          AND x <= y
   UNION ALL
   SELECT c2.id
         ,LPAD(SUBSTR(c1.cover,1,25)+SUBSTR(c2.cover,1,25),25,'0')   ---- 位数太多NUMBER型不支持,所以拆成两截分别相加再拼回来
          ||LPAD(SUBSTR(c1.cover,26)+SUBSTR(c2.cover,26),24,'0')
         ,result||'('||c2.x||','||c2.y||')'
         ,c1.lvl+1
     FROM t c1,cells c2
    WHERE c1.lvl<4
          AND c1.id<c2.id AND SUBSTR(c1.cover,c2.id,1)='0'
  )
SELECT result FROM t
WHERE lvl=4
       AND INSTR(cover,'0')=0;


RESULT
----------------------------
(2,2)(5,3)(1,6)(4,7)
(3,3)(7,4)(2,6)(6,7)

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
43#
 楼主| 发表于 2010-8-19 01:46 | 只看该作者
http://www.puzzles.com/puzzleplayground/UnattackedCells/UnattackedCells.htm

Unattacked Cells
after Martin Gardner
Home / Puzzle Playground / Puzzles / Chess 'n' Checkers /
It's quite easy to place four chess queens on a 4x4 board to leave one unattacked vacant cell - see the diagram at right. The only unattacked cell is marked with a red dot. This cell can't be reached by any of the queens according to their allowed moves.

Your task is to place five chess queens on a 5x5 board in such a way that a maximum number of vacant cells are unattacked.

By the way, if you've forgotten the chess queen's moves the diagram at left will remind it to you. It shows that you may move the queen in any of the eight directions and on any number of the vacant cells.

For more convenient solving you may print out the board, and use five quarters as queens and some pennies to mark the unattacked vacant cells.
新任务:
5X5的棋盘上摆放5个皇后,使得它们攻击不到的安全点最多(各皇后之间可以互相攻击)。

使用道具 举报

回复
招聘 : Java研发
论坛徽章:
71
马上加薪
日期:2014-02-19 11:55:14蜘蛛蛋
日期:2012-12-26 18:16:01茶鸡蛋
日期:2012-11-16 08:12:48ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07奥运会纪念徽章:网球
日期:2012-08-23 14:58:08奥运会纪念徽章:沙滩排球
日期:2012-07-19 17:28:14版主2段
日期:2012-07-07 02:21:02咸鸭蛋
日期:2012-03-23 18:17:482012新春纪念徽章
日期:2012-02-13 15:13:512012新春纪念徽章
日期:2012-02-13 15:13:51
44#
发表于 2010-8-19 07:27 | 只看该作者
可以试试Oracle Spatial

使用道具 举报

回复
论坛徽章:
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
45#
发表于 2010-8-19 07:36 | 只看该作者
我猜把棋子放到角上挤成一团会好

[ 本帖最后由 〇〇 于 2010-8-19 07:42 编辑 ]

UnattackedCells1.gif (7.38 KB, 下载次数: 83)

UnattackedCells1.gif

使用道具 举报

回复
论坛徽章:
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
46#
发表于 2010-8-19 09:03 | 只看该作者
猜错了

UnattackedCells2.gif (7.81 KB, 下载次数: 69)

UnattackedCells2.gif

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
47#
发表于 2010-8-19 09:09 | 只看该作者
hehe
论性能,还是newkid厉害。

不过,我写的时候的是这样考虑的:
1。 首先,大家能看懂
2。 然后,跑得快

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
48#
发表于 2010-8-19 09:24 | 只看该作者
五皇后



6个红圈处任选一处摆放,仅能消除掉中路三个未被攻击的单元格中的一个
若在其他位置摆放,则能消除掉中路三个未被攻击的单元格中的两个或三个(即全部)

此摆法最多剩余 2个未被攻击的单元格

使用道具 举报

回复
论坛徽章:
32
奥运会纪念徽章:摔跤
日期:2012-08-23 11:03:05青年奥林匹克运动会-击剑
日期:2014-09-19 10:58:152014年世界杯参赛球队:巴西
日期:2014-07-07 12:19:232014年世界杯参赛球队: 瑞士
日期:2014-05-19 12:18:36马上有钱
日期:2014-04-08 12:12:232014年新春福章
日期:2014-04-04 14:20:47马上有钱
日期:2014-02-18 16:43:092014年新春福章
日期:2014-02-18 16:43:09红旗
日期:2014-02-14 15:15:55优秀写手
日期:2013-12-18 09:29:16
49#
发表于 2010-8-19 09:55 | 只看该作者
看能不能摆下6个皇后,我sql出来是不能,sql不贴了


猜下,5*5棋盘最多只能放下5个皇后?
那么位置是这些,没有管对称性的:
SQL> with temp as (
  2  select level node,
  3         trunc((level+4)/5) -1 row_num,
  4         mod(level+4,5) col_num,
  5         case when mod(level,5) >0 then  mod(level+4,5)+trunc(level/5)
  6              when mod(level,5) = 0 then mod(level+4,5)+trunc(level/5 -1)
  7         end a_num,
  8         level -6*(trunc((level+4)/5) -1)+12   b_num
  9   from dual connect by level <= 25
10  )
11  select a.node,b.node,c.node,d.node,e.node
12      from temp a ,temp b ,temp c,temp d,temp e
13  where a.node < b.node
14      and b.node < c.node
15      and c.node < d.node
16      and d.node < e.node
17      and not (
18          a.row_num = b.row_num or a.row_num = c.row_num or a.row_num = d.row_num or a.row_num = e.row_num
19          or b.row_num = c.row_num or b.row_num = d.row_num or b.row_num = e.row_num
20          or c.row_num = d.row_num or c.row_num = e.row_num
21          or d.row_num = e.row_num
22          )
23      and not (
24          a.col_num = b.col_num or a.col_num = c.col_num or a.col_num = d.col_num or a.col_num = e.col_num
25          or b.col_num = c.col_num or b.col_num = d.col_num or b.col_num = e.col_num
26          or c.col_num = d.col_num or c.col_num = e.col_num  
27          or d.col_num = e.col_num
28          )
29      and not (
30          a.a_num = b.a_num or a.a_num = c.a_num or a.a_num = d.a_num or a.a_num = e.a_num
31          or b.a_num = c.a_num or b.a_num = d.a_num or b.a_num = e.a_num
32          or c.a_num = d.a_num or c.a_num = e.a_num
33          or d.a_num = e.a_num
34          )
35      and not (
36          a.b_num = b.b_num or a.b_num = c.b_num or a.b_num = d.b_num or a.b_num = e.b_num
37          or b.b_num = c.b_num or b.b_num = d.b_num or b.b_num = e.b_num
38          or c.b_num = d.b_num or c.b_num = e.b_num
39          or d.b_num = e.b_num
40          )
41  ;

      NODE       NODE       NODE       NODE       NODE
---------- ---------- ---------- ---------- ----------
         1          8         15         17         24
         1          9         12         20         23
         2          9         11         18         25
         2         10         13         16         24
         3          6         14         17         25
         3         10         12         19         21
         4          6         13         20         22
         4          7         15         18         21
         5          7         14         16         23
         5          8         11         19         22

10 rows selected.

SQL>

[ 本帖最后由 yellowlee 于 2010-8-19 09:58 编辑 ]

使用道具 举报

回复
论坛徽章:
11
2010新春纪念徽章
日期:2010-03-01 11:08:27SQL大赛参与纪念
日期:2011-04-13 12:08:172010广州亚运会纪念徽章:空手道
日期:2011-03-08 15:29:592011新春纪念徽章
日期:2011-02-18 11:43:362010广州亚运会纪念徽章:台球
日期:2011-01-26 10:41:28数据库板块每日发贴之星
日期:2010-12-10 01:01:022010广州亚运会纪念徽章:网球
日期:2010-12-09 13:11:342010广州亚运会纪念徽章:篮球
日期:2010-12-06 14:28:04辩论纪念章
日期:2010-11-15 10:46:13ITPUB9周年纪念徽章
日期:2010-10-08 09:28:52
50#
发表于 2010-8-19 09:58 | 只看该作者

回复 #37 newkid 的帖子

俺写的性能最差....
第二个题直接摆比用SQL快N多

使用道具 举报

回复

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

本版积分规则 发表回复

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