在一个2k x 2k ( 即:2^k x 2^k )个方格组成的棋盘中,恰有一个方格与其他方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。 【输入形式】 输入文件第一行为整数k,(棋盘大小是2k*2k 1<=k<=20),第二行是两个整数,代表特殊方格所在行号和列号。 【输出形式】 特殊点用0输出,数据间用制表符隔开(‘t’), 要求遍历顺序按从左到右,从上到下。具体看样例
时间: 2023-09-27 12:06:18 浏览: 66
好的,我了解了你的问题。这是一个棋盘覆盖问题,需要用L型骨牌覆盖棋盘上除了特殊方格以外的所有方格。我会根据输入的k和特殊方格的位置,输出覆盖后的棋盘。下面是代码实现:
```python
import sys
# 用于记录L型骨牌的位置
board = [[0 for i in range(2**k)] for j in range(2**k)]
# 用于标记特殊方格的位置
def fill(x, y, s, tx, ty):
global board
if s == 1:
return
t = s // 2
cnt = 0
if tx < x + t and ty < y + t:
cnt += 1
fill(x, y, t, tx, ty)
else:
board[x+t-1][y+t-1] = cnt
if tx < x + t and ty >= y + t:
cnt += 1
fill(x, y+t, t, tx, ty)
else:
board[x+t-1][y+t] = cnt
if tx >= x + t and ty < y + t:
cnt += 1
fill(x+t, y, t, tx, ty)
else:
board[x+t][y+t-1] = cnt
if tx >= x + t and ty >= y + t:
cnt += 1
fill(x+t, y+t, t, tx, ty)
else:
board[x+t][y+t] = cnt
def cover(x, y, s, fx, fy):
global board
global L
if s == 1:
return
t = s // 2
cnt = board[x+t-1][y+t-1]
if fx < x+t and fy < y+t:
cover(x, y, t, fx, fy)
else:
board[x+t-1][y+t-1] = cnt
cover(x, y, t, x+t-1, y+t-1)
cnt = board[x+t-1][y+t]
if fx < x+t and fy >= y+t:
cover(x, y+t, t, fx, fy)
else:
board[x+t-1][y+t] = cnt
cover(x, y+t, t, x+t-1, y+t)
cnt = board[x+t][y+t-1]
if fx >= x+t and fy < y+t:
cover(x+t, y, t, fx, fy)
else:
board[x+t][y+t-1] = cnt
cover(x+t, y, t, x+t, y+t-1)
cnt = board[x+t][y+t]
if fx >= x+t and fy >= y+t:
cover(x+t, y+t, t, fx, fy)
else:
board[x+t][y+t] = cnt
cover(x+t, y+t, t, x+t, y+t)
# 输出结果
def print_board(s, tx, ty):
global board
for i in range(s):
for j in range(s):
if i == tx and j == ty:
sys.stdout.write('0\t')
else:
sys.stdout.write(str(board[i][j])+'\t')
sys.stdout.write('\n')
if __name__ == '__main__':
k = int(input().strip())
tx, ty = map(int, input().strip().split())
s = 2**k
fill(0, 0, s, tx, ty)
L = 0
cover(0, 0, s, tx, ty)
print_board(s, tx, ty)
```
你可以将上面的代码复制到本地进行测试,输入k和特殊方格的位置,即可得到覆盖后的棋盘。注意:输入格式必须按照要求输入,否则代码可能会出错。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)