Python3 implementation of Board coverage

Source: Internet
Author: User

In a checkerboard consisting of 2 ^ k * 2 ^ k Square, there is a square occupied, and the four L-shaped bone cards are used to cover all the squares on all the checkerboards, which cannot overlap.


The Code is as follows:

def chess(tr,tc,pr,pc,size):global mark global tablemark+=1count=markif size==1:returnhalf=size//2if pr<tr+half and pc<tc+half:chess(tr,tc,pr,pc,half)else:table[tr+half-1][tc+half-1]=countchess(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]=countchess(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]=countchess(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]=countchess(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=0n=8table=[[-1 for x in range(n)] for y in range(n)]chess(0,0,2,2,n)show(table)

N is the checkerboard width and must be 2 ^ k. n = 8 in this example. The special lattice is at (2, 2), as shown in:


Divide and conquer the Board into four parts each time. If the special lattice is in this small board, it is further divided into four parts, if it is not in this small board, place the lattice located near the center of the Small board, indicating that 1/3 of the L-type dominoes occupies this position. Each recursion will traverse and query four small boards, the three grids with no special checkerboard positions constitute a complete L-shaped card in the center of the large checkerboard, and so on. The running result is as follows:



Reprinted Please note:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.