''' 0401 PA05 21800279 Seonyeong Park Polyomino Puzzle: To find a solution of puzzle for given polyominos ''' import numpy as np import math import copy #input n = int(input()) blocks = [] count = 0 for i in range(n): h,w = [int(x) for x in input().split()] blocks.append([(h,w)]) for j in range(h): line = [int(x) for x in input().split()] for b in line: if b == 1: count+=1 blocks[i].append(line) def check(c): if s == int(s): for block in blocks: for line in block[0]: if line > s: return False return int(s) return False def puzzle(num,ans,row,col): b = blocks[num] h,w = b[0] res = copy.deepcopy(ans) for i in range(h): for j in range(w): if b[i+1][j] == 1: if ans[row+i][col+j] == 0: res[row+i][col+j] = num+1 else: return False for i in range(side): for j in range(side): if res[i][j] == 0: return [res, i, j] return [res, i, j] def polyomino(pro,ans,num=0,row=0,col=0,put=[],turn=0): print(ans) if len(put) == n: return ans if turn == n: return False if num == n: num -= n block = blocks[num] if num in put: if num == put[-1]: print("A", end = ' ') ans, row, col = copy.deepcopy(pro[len(put)-1]) if num == turn: turn +=1 return polyomino(pro[:-1], ans, num+1,row,col,put[:-1],turn) else: print("B",end=" ") return polyomino(pro,ans,num+1,row,col,put,turn) h,w = block[0] print(num,"w=%d, h=%d"%(col,row)) print(put) if row+h > side: print("C",end = " ") return polyomino(pro,ans,num+1,row,col,put,turn) else: start = 0 if col+1-w <= 0 else col+1-w result = puzzle(num,ans,row,start) while result == False and start+w < side : start += 1 result = puzzle(num,ans,row,start) if result == False: print("D",end = " ") return polyomino(pro,ans,num+1,row,col,put,turn) else: put.append(num) pro.append(result) ans, row, col = result print("E") print(result) return polyomino(pro,ans, num+1,row,col,put,turn) #---------------------------------------------------------------------------------# side = check(count) #the side of the panel if side == False: print("No solution possible") else: process = [] answer = np.zeros([side,side], dtype="i").tolist() process.append([copy.deepcopy(answer),0,0]) answer = polyomino(process,answer) if answer == False: print("No solution possible") else: for line in answer: print(' '.join(map(str, line)))
3 3 2 1 1 0 1 0 1 2 1 1 1 3 1 1 1 1
[[0, 0, 0], [0, 0, 0], [0, 0, 0]] 0 w=0, h=0 [] E [[[1, 1, 0], [0, 1, 0], [0, 1, 0]], 0, 2] [[1, 1, 0], [0, 1, 0], [0, 1, 0]] 1 w=2, h=0 [0] E [[[1, 1, 2], [0, 1, 2], [0, 1, 0]], 1, 0] [[1, 1, 2], [0, 1, 2], [0, 1, 0]] 2 w=0, h=1 [0, 1] C [[1, 1, 2], [0, 1, 2], [0, 1, 0]] B [[1, 1, 2], [0, 1, 2], [0, 1, 0]] A [[1, 1, 0], [0, 1, 0], [0, 1, 0]] 2 w=2, h=0 [0] E [[[1, 1, 3], [0, 1, 3], [0, 1, 3]], 1, 0] [[1, 1, 3], [0, 1, 3], [0, 1, 3]] B [[1, 1, 3], [0, 1, 3], [0, 1, 3]] 1 w=0, h=1 [0, 2] E [[[1, 1, 3], [2, 1, 3], [2, 1, 3]], 2, 2] [[1, 1, 3], [2, 1, 3], [2, 1, 3]] 1 1 3 2 1 3 2 1 3