Incomplete checkerboard covering

  Cover a checkerboard that has one corner square missing. The size of the board is usuall 2**k, use recursive way the board will be

divided into four 2**(k-1) size small board. Cover the center board with a L shape tile, the one fourth board will the same as the original

board.

 

(r, c) is the left top corner coordinate. s is size / 2.



 

 

tile = 0 # global variable for the L shape tile counting

'''
    brow, bcol is the board's up left corner coordinate
    mrow, mcol is the missing tile's coordinate
    size is the board size
    2**k size board will be divided to four 2**(k-1) size sub-board
    if the missing tile is one fourth board, the L shape will be put
    in the other three one fourth board's center corner
'''
def cover(board, brow, bcol, mrow, mcol, size):
    if size is 1: 
        return
    
    global tile
    tile += 1
    count = tile  # L shape tile number
    s = size // 2  # divide the board
    
    # cover the upper left board
    if mrow < brow + s and mcol < bcol + s:  # if the missing tile is in this square
        cover(board, brow, bcol, mrow, mcol, s)
    else:
        board[brow + s - 1][bcol + s - 1] = count  # cover the down right corner with L shape
        cover(board, brow, bcol, brow + s - 1, bcol + s - 1, s)
    
    # cover the upper right board
    if mrow < brow + s and mcol >= bcol + s:  # if the missing tile is in this square
        cover(board, brow, bcol + s, mrow, mcol, s)
    else:
        board[brow + s - 1][bcol + s] = count  # cover the left down corner with L shape
        cover(board, brow, bcol +s, brow + s - 1, bcol +s, s)
    
    # cover the down left board
    if mrow >= brow + s and mcol < bcol + s:  # if the missing tile is in this square
        cover(board, brow + s, bcol, mrow, mcol, s)
    else:
        board[brow + s][bcol + s - 1] = count  # cover the up right corner with L shape
        cover(board, brow + s, bcol, brow + s, bcol + s - 1, s)
        
    # cover the down right board
    if mrow >= brow + s and mcol >= bcol + s:  # if the missing tile is in this square
        cover(board, brow +s, bcol + s, mrow, mcol, s)
    else:
        board[brow + s][bcol + s] = count  # cover the up left corner with L shape
        cover(board, brow + s, bcol + s, brow + s, bcol + s, s)    
    
def main():
    board = [[0] * 8 for i in range(8)] # 8 * 8 board
    board[7][7] = -1 # the missing corner
    cover(board, 0, 0, 7, 7, len(board))
    
    for row in board:
        print ' %2i' * 8 % tuple(row)
    
if __name__ == '__main__':
    main()
    


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值