new file 100644
EMPTY="."
FULL="O"
class Piece:
def __init__(self,variants):
self.variants=variants
pieces=[]
pieces.append(Piece([
# OO
((0,0),(0,1),(1,0),(1,1))
]))
# OOOO
((0,0),(0,1),(0,2),(0,3)),
((0,0),(1,0),(2,0),(3,0))
# OOO
# .O.
((0,0),(0,1),(0,2),(1,1)),
((0,0),(1,0),(2,0),(1,1)),
((0,0),(1,-1),(1,0),(1,1)),
((0,0),(1,-1),(1,0),(2,0))
# .OO
# OO.
((0,0),(0,1),(1,-1),(1,0)),
((0,0),(1,0),(1,1),(2,1)),
((0,0),(0,1),(1,1),(1,2)),
((0,0),(1,0),(1,-1),(2,-1))
# O..
((0,0),(1,0),(0,1),(0,2)),
((0,0),(1,0),(2,0),(2,1)),
((0,0),(1,-2),(1,-1),(1,0)),
((0,0),(0,1),(1,1),(2,1)),
# ..O
((0,0),(0,1),(0,2),(1,2)),
((0,0),(0,1),(1,0),(2,0)),
((0,0),(1,0),(1,1),(1,2)),
((0,0),(1,0),(2,0),(2,-1))
WIDTH=5
HEIGHT=5
board=[[EMPTY]*WIDTH for r in range(HEIGHT)]
def fill(board):
point=firstEmpty(board)
if not point: return True
res=[]
for p in pieces:
for v in p.variants:
if place(board,point,v):
partialRes=fill(board)
if partialRes:
res+=[((point,v), partialRes)]
remove(board,point,v)
return res
def firstEmpty(board):
for (r,row) in enumerate(board):
for (c,cell) in enumerate(row):
if cell==EMPTY:
return (r,c)
return None
def place(board,point,piece):
(r,c)=point
if not all(0<=r+ri<HEIGHT and 0<=c+ci<WIDTH and board[r+ri][c+ci]==EMPTY for (ri,ci) in piece):
return False
for (ri,ci) in piece:
board[r+ri][c+ci]=FULL
return True
def remove(board,point,piece):
board[r+ri][c+ci]=EMPTY
def visualise(board,tree,letter="A"):
if tree is True:
for row in board:
print("".join(row))
print()
return
for (((r,c),piece),rest) in tree:
board[r+ri][c+ci]=letter
visualise(board,rest,chr(ord(letter)+1))
res=fill(board)
if res:
visualise(board,res)
else:
print(res)
Status change: