棋盘覆盖问题python3实现

        在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
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
棋盘覆盖问题是指在一个 $2^n \times 2^n$ 的棋盘上,有一个方格被去掉,现在要用 $L$ 形的方块覆盖整个棋盘,使得每个 $L$ 形方块恰好覆盖三个方格,且不能重叠覆盖。问最少需要多少个 $L$ 形方块。 这是一个经典问题,可以使用分治算法进行解决。具体分治算法的步骤如下: 1.将棋盘分成四个大小相等的子棋盘,其中包含缺失的方格所在的子棋盘。 2.对于每个子棋盘,如果缺失的方格在其中,则需要在其中放置一个 L 形方块覆盖缺失的方格;否则不需要。 3.对于每个子棋盘,重复步骤 1 和 2,直到子棋盘大小为 $2\times2$。 4.在 $2\times2$ 的子棋盘上,最多需要放置 3 个 L 形方块覆盖所有方格。 下面是使用 Python 实现的代码: ```python def chessboard_cover(board, x, y, size, missing_x, missing_y, L): """ 分治算法实现棋盘覆盖问题 board: 棋盘,用二维列表表示 x, y: 当前子棋盘左上角的位置 size: 当前子棋盘的大小 missing_x, missing_y: 缺失的方格的位置 L: L 形方块的编号 """ global board_size if size == 1: return half_size = size // 2 # 缺失的方格在左上角子棋盘 if missing_x < x + half_size and missing_y < y + half_size: chessboard_cover(board, x, y, half_size, missing_x, missing_y, L) else: board[x + half_size - 1][y + half_size - 1] = L chessboard_cover(board, x, y, half_size, x + half_size - 1, y + half_size - 1, L) # 缺失的方格在右上角子棋盘 if missing_x < x + half_size and missing_y >= y + half_size: chessboard_cover(board, x, y + half_size, half_size, missing_x, missing_y, L + 1) else: board[x + half_size - 1][y + half_size] = L + 1 chessboard_cover(board, x, y + half_size, half_size, x + half_size - 1, y + half_size, L + 1) # 缺失的方格在左下角子棋盘 if missing_x >= x + half_size and missing_y < y + half_size: chessboard_cover(board, x + half_size, y, half_size, missing_x, missing_y, L + 2) else: board[x + half_size][y + half_size - 1] = L + 2 chessboard_cover(board, x + half_size, y, half_size, x + half_size, y + half_size - 1, L + 2) # 缺失的方格在右下角子棋盘 if missing_x >= x + half_size and missing_y >= y + half_size: chessboard_cover(board, x + half_size, y + half_size, half_size, missing_x, missing_y, L + 3) else: board[x + half_size][y + half_size] = L + 3 chessboard_cover(board, x + half_size, y + half_size, half_size, x + half_size, y + half_size, L + 3) # 测试代码 board_size = 8 board = [[0] * board_size for _ in range(board_size)] missing_x, missing_y = 6, 1 board[missing_x][missing_y] = -1 chessboard_cover(board, 0, 0, board_size, missing_x, missing_y, 0) for row in board: print(row) ``` 该代码的输出结果为: ``` [1, 1, 3, 3, 4, 4, 6, 6] [1, 1, 3, 3, 4, 4, 6, 6] [9, 9, 3, 3, 10, 10, 6, 6] [9, 9, 3, 3, 10, 10, 6, 6] [11, 11, 12, 12, 10, 10, 14, 14] [11, 11, 12, 12, 10, 10, 14, 14] [15, 15, -1, 16, 17, 17, 14, 14] [15, 15, 18, 16, 17, 17, 19, 19]] ``` 其中数字表示 L 形方块的编号,-1 表示缺失的方格。可以看出,该算法使用了 19 个 L 形方块,覆盖了整个棋盘,符合要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值