-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckerBoard.py
More file actions
29 lines (25 loc) · 787 Bytes
/
CheckerBoard.py
File metadata and controls
29 lines (25 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def cover(board, lab=1, top=0, left=0, side=None):
if side is None:
side = len(board)
# Side length of subboard:
s = side // 2
# Offsets for outer/inner squares of subboards:
offsets = (0, -1), (side-1, 0)
for dy_outer, dy_inner in offsets:
for dx_outer, dx_inner in offsets:
# If the outer corner is not set...
if not board[top+dy_outer][left+dx_outer]:
# ... label the inner corner:
board[top+s+dy_inner][left+s+dx_inner] = lab
# Next label:
lab += 1
if s > 1:
for dy in [0, s]:
for dx in [0, s]:
# Recursive calls, if s is at least 2:
lab = cover(board, lab, top+dy, left+dx, s)
# Return the next available label:
return lab
board = [[0]*8 for i in range(8)] # Eight by eight checkerboard >>>
board[7][7] = -1 # Missing corner
cover(board)