在2^k*2^k个方格组成的棋盘中,有一个方格被占用,用下图的4种L型骨牌覆盖全部棋盘上的其余全部方格,不能重叠。

代码例如以下:

def chess(tr,tc,pr,pc,size):
global mark
global table
mark+=1
count=mark
if size==1:
return
half=size//2
if pr<tr+half and pc<tc+half:
chess(tr,tc,pr,pc,half)
else:
table[tr+half-1][tc+half-1]=count
chess(tr,tc,tr+half-1,tc+half-1,half)
if pr<tr+half and pc>=tc+half:
chess(tr,tc+half,pr,pc,half)
else:
table[tr+half-1][tc+half]=count
chess(tr,tc+half,tr+half-1,tc+half,half)
if pr>=tr+half and pc<tc+half:
chess(tr+half,tc,pr,pc,half)
else:
table[tr+half][tc+half-1]=count
chess(tr+half,tc,tr+half,tc+half-1,half)
if pr>=tr+half and pc>=tc+half:
chess(tr+half,tc+half,pr,pc,half)
else:
table[tr+half][tc+half]=count
chess(tr+half,tc+half,tr+half,tc+half,half) def show(table):
n=len(table)
for i in range(n):
for j in range(n):
print(table[i][j],end=' ')
print('') mark=0
n=8
table=[[-1 for x in range(n)] for y in range(n)]
chess(0,0,2,2,n)
show(table)

n是棋盘宽度。必须是2^k,本例中n=8,特殊格子在(2,2)位置,例如以下图所看到的:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl0dGxldGh1bmRlcg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

採用分治法每次把棋盘分成4份,假设特殊格子在这个小棋盘中则继续分成4份,假设不在这个小棋盘中就把该小棋盘中靠近中央的那个格子置位。表示L型骨牌的1/3占领此处。每一次递归都会遍历查询4个小棋盘,三个不含有特殊格子的棋盘置位的3个格子正好在大棋盘中央构成一个完整的L型骨牌,依次类推,找到所有覆盖方法。

执行结果例如以下:

转载请注明:转自http://blog.csdn.net/littlethunder/article/details/25288969

最新文章

  1. MySQL 用户登录密码和远程登录权限问题
  2. php file_get_contents失败[function.file-get-contents]: failed to open stream: HTTP request failed!解决
  3. DSP using MATLAB 示例 Example3.13
  4. System.arraycopy
  5. 九度oj 题目1034:寻找大富翁
  6. ACL权限的学习
  7. img图片之间的间距问题
  8. Chrome 应用推荐 - 下载管理扩展: Chrono
  9. .NET 认识
  10. BootStrap入门_创建第一个例子
  11. Servlet的一些细节(2)
  12. 如何修改mtk android 默认拍照size
  13. Inno Setup
  14. Brave Game(裸的巴什博弈)
  15. 从PRISM开始学WPF(四)Prism-Module-更新至Prism7.1
  16. LINUX监控-spotlight
  17. [git] git怎样fork一个repo
  18. UVALive-7041(回文树
  19. hdu 1272 判断所给的图是不是生成树 (并查集)
  20. CentOS7LINUX 内核调试符号安装

热门文章

  1. Cocos Code IDE
  2. OEM:Enterprise Manager 无法连接到数据库实例 错误解决
  3. Docker容器查看ip地址
  4. php开启CURL支持
  5. WINDOWS系统注册表(读、写)
  6. Java NIO(七)管道
  7. 问题集锦 ~ PHP
  8. 从Dinnr失败看产品市场可行性认知有哪些不足
  9. QA小课堂:一个网站或者APP开发要多少钱
  10. 页面定制CSS代码初探(一):页面变宽 文本自动换行 图片按比缩放