分治算法求解棋盘覆盖问题

本文介绍了一个特殊的2k×2k棋盘,其中只有一个方格不同,目标是用L型骨牌覆盖所有非特殊方格,不重叠。通过分治策略,将棋盘划分为4个2k-1×2k-1的子棋盘,并根据特殊方格位置进行四种情况的递归填充。详细题解和代码实现参考链接。
摘要由CSDN通过智能技术生成

1. 问题描述:

在一个2k×2k 个方格组成的棋盘中,恰有一个方格与其它方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。
在这里插入图片描述

2. 题解:

在这里插入图片描述
划分问题:将 2k∗2k的棋盘划分为 2k−1∗2k−1这样的子棋盘4块。
递归求解:递归填充各个格子,填充分为四个情况,在下面会有解释,递归出口为 k=0也就是子棋盘方格数为1。
递归填充的分为以下四种情况:
(1)如果黑方块在左上子棋盘,则递归填充左上子棋盘;否则填充左上子棋盘的右下角,将右下角看做黑色方块

棋盘覆盖问题是指在一个2^n * 2^n的棋盘上,恰好有一个方格与其他方格不同,现在要用L型骨牌盖整个棋盘,求盖方案。分治算法是一种将问题分解成多个子问题来解决的算法,可以用来解决棋盘覆盖问题。具体步骤如下: 1.将棋盘分成四个大小相等的子棋盘,其中一个子棋盘包含特殊方格。 2.用L型骨牌盖其他三个子棋盘,递归地解决这三个子棋盘棋盘覆盖问题。 3.递归地解决包含特殊方格的子棋盘棋盘覆盖问题。 下面是Python程序实现分治算法求解棋盘覆盖问题的代码: ```python def chessboard_cover(board, tr, tc, dr, dc, size, special_row, special_col): global tile if size == 1: return t = tile tile += 1 s = size // 2 # 判断特殊方格所在的子棋盘 if special_row < tr + s and special_col < tc + s: chessboard_cover(board, tr, tc, special_row, special_col, s, special_row, special_col) else: board[tr + s - 1][tc + s - 1] = t chessboard_cover(board, tr, tc, tr + s - 1, tc + s - 1, s, tr + s - 1, tc + s - 1) if special_row < tr + s and special_col >= tc + s: chessboard_cover(board, tr, tc + s, special_row, special_col, s, special_row, special_col) else: board[tr + s - 1][tc + s] = t chessboard_cover(board, tr, tc + s, tr + s - 1, tc + s, s, tr + s - 1, tc + s) if special_row >= tr + s and special_col < tc + s: chessboard_cover(board, tr + s, tc, special_row, special_col, s, special_row, special_col) else: board[tr + s][tc + s - 1] = t chessboard_cover(board, tr + s, tc, tr + s, tc + s - 1, s, tr + s, tc + s - 1) if special_row >= tr + s and special_col >= tc + s: chessboard_cover(board, tr + s, tc + s, special_row, special_col, s, special_row, special_col) else: board[tr + s][tc + s] = t chessboard_cover(board, tr + s, tc + s, tr + s, tc + s, s, tr + s, tc + s) # 测试代码 size = 8 board = [[0 for j in range(size)] for i in range(size)] tile = 1 special_row = 6 special_col = 6 chessboard_cover(board, 0, 0, special_row, special_col, size, special_row, special_col) for i in range(size): for j in range(size): print(board[i][j], end='\t') print() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天使Di María

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值