# Author: Manfred Scheucher # Date : 07.07.2015 from sys import argv from copy import copy areas = [] def placeH(grid,stuff,x,y): grid2 = copy(grid) stuff2 = copy(stuff) grid2.append((x,y)) grid2.append((x+1,y)) stuff2[(x,y)] = 'H' return grid2, stuff2 def placeV(grid,stuff,x,y): grid2 = copy(grid) stuff2 = copy(stuff) grid2.append((x,y)) grid2.append((x,y+1)) stuff2[(x,y)] = 'V' return grid2, stuff2 def check(grid,stuff,k): if stuff: x0 = min([x for (x,y) in grid]) x1 = max([x for (x,y) in grid]) y0 = min([y for (x,y) in grid]) y1 = max([y for (x,y) in grid]) else: x0=x1=y0=y1=0 toprint = {(x,y):' ' for x in range(x0,x1+1) for y in range(y0,y1+1)} for (x,y) in stuff: toprint[(x,y)] = stuff[(x,y)] if stuff[(x,y)]=='H': toprint[(x+1,y)] = '-' else: toprint[(x,y+1)] = '|' global solutions area = set(xy for xy in toprint if toprint[xy]!=' ') if area in solutions[k]: return False # already counted solutions[k].append(area) if k==n and DEBUG: print "sol #",len(solutions[k]),":",area#,grid2 print for y in range(y0,y1+1): for x in range(x0,x1+1): print toprint[(x,y)], print print return True def normalize(grid,stuff): if not grid: return grid, stuff x0 = min([x for (x,y) in grid]) y0 = min([y for (x,y) in grid]) grid2 = [(x-x0,y-y0) for (x,y) in grid] stuff2 = {(x-x0,y-y0):stuff[(x,y)] for (x,y) in stuff} return grid2, stuff2 def is_valid(stuff,last): x1,y1 = last direction = stuff[(x1,y1)] for (x2,y2) in stuff: c = abs(x2-x1)+abs(y2-y1) d = abs(x2-x1)*abs(y2-y1) if c == 1 and d == 0 and stuff[(x2,y2)]==direction: #print "ABBORT",stuff,(x1,y1),(x2,y2) return False return True def place(k=0,grid=[],stuff=dict(),last=None): if last and not is_valid(stuff,last): return grid,stuff = normalize(grid,stuff) if stuff not in solutions[k]: if not check(grid,stuff,k): return if k < n: if not grid: grid2,stuff2 = placeH(grid,stuff,0,0) place(k+1,grid2,stuff2) grid2,stuff2 = placeV(grid,stuff,0,0) place(k+1,grid2,stuff2) else: for (x,y) in grid: for (dx,dy) in [(-2,0),(1,0),(-1,-1),(0,-1),(-1,1),(0,1)]: # horizontal placement (x2,y2) = (x+dx,y+dy) if (x2,y2) not in grid and (x2+1,y2) not in grid: grid2,stuff2 = placeH(grid,stuff,x2,y2) place(k+1,grid2,stuff2,last=(x2,y2)) # vertical placement (mirrored possibilies) (x2,y2) = (x+dy,y+dx) if (x2,y2) not in grid and (x2,y2+1) not in grid: grid2,stuff2 = placeV(grid,stuff,x2,y2) place(k+1,grid2,stuff2,last=(x2,y2)) DEBUG = 0 n = int(argv[1]) solutions = {k: [] for k in range(0,n+1)} place() for k in solutions: print "a("+str(k)+")="+str(len(solutions[k]))