Rezultati

Up. imeNalogaJezikRezultatČas oddaje
vlakectomaz-2018 Puzzle Python 3 100/100OK 19. apr '18 @ 18:54

Test Točke Porabljen spomin Porabljen čas Status
#1 10/10 8,332 MiB 0,000 s OK
#2 10/10 8,430 MiB 0,000 s OK
#3 10/10 8,336 MiB 0,000 s OK
#4 10/10 8,438 MiB 0,000 s OK
#5 10/10 8,426 MiB 0,000 s OK
#6 10/10 8,449 MiB 0,000 s OK
#7 10/10 8,449 MiB 0,000 s OK
#8 10/10 8,438 MiB 0,000 s OK
#9 10/10 8,422 MiB 0,000 s OK
#10 10/10 8,363 MiB 0,000 s OK

Ocenjevani program (puzzle.py):
class Koscek:
    # 0 left, 1 top, 2 right, 3 bottom
    def __init__(self, sides):
        self.sides_orig = sides
        self.set1 = set(sides)
        self.set2 = set("".join(x) for x in zip(sides, sides[1:]+sides[:1]))
        self.sides = sides
        
        
    def rotate(self,am):
        am = -am
        self.sides =  self.sides_orig[am:] + self.sides_orig[:am]
        
    def candidates(self, side, char):
        candidates=[]
        for i in range(4):
            c = self.sides_orig[i]
            if(c==char):
                candidates.append((side-i+4)%4)
        return candidates
        
    def __repr__(self):
        return repr(self.sides)
        

def invert(char):
    if(char.islower()):
        return char.upper()
    else:
        return char.lower()

koscki = [Koscek(list(input())) for _ in range(9)]

field = [[-1 for _ in range(3)] for _ in range(3)]

used = [False for _ in koscki]

# y, x
coord={
    1: (1,1),
    2: (1,0),
    3: (0,1),
    4: (1,2),
    5: (2,1),
    6: (0,0),
    7: (0,2),
    8: (2,2),
    9: (2,0)
}

# side
sides={
    2: 2,
    3: 3,
    4: 0,
    5: 1
}

# num1, side1, num2, side2
corners={
    6: (3,0,2,1),
    7: (4,1,3,2),
    8: (5,2,4,3),
    9: (2,3,5,0)
}

def rekurzija(step):
    if(step==1):
        for i in range(9):
            field[1][1] = i
            used[i] = True
            if(rekurzija(step+1)):
                return True
            
            used[i] = False
        return False
    elif(step < 6):
        y,x = coord[step]
        side = sides[step]
        side_c = (side+2) % 4
        
        char_c = koscki[field[1][1]].sides[side_c]
        char_need = invert(char_c)
        kandidati = [i for i in range(9) if not used[i] and char_need in koscki[i].set1]
        for k in kandidati:
            kos = koscki[k]
            rots = kos.candidates(side,char_need)
            used[k] = True
            for r in rots:
                kos.rotate(r)
                field[y][x] = k
                
                if(rekurzija(step+1)):
                    return True
            
            used[k] = False
        return False
    elif(step < 10):
        y,x = coord[step]
        n1,side1,n2,side2 = corners[step]
        y1,x1 = coord[n1]
        y2,x2 = coord[n2]
        c1 = invert(koscki[field[y1][x1]].sides[side1])
        c2 = invert(koscki[field[y2][x2]].sides[side2])
        niz = c1+c2
        kandidati = [i for i in range(9) if not used[i] and niz in koscki[i].set2]
        for k in kandidati:
            kos = koscki[k]
            rot1 = kos.candidates((side1+2)%4, c1)
            rot2 = kos.candidates((side2+2)%4, c2)
            cand = [r for r in rot1 if r in rot2]
            
            kos.rotate(cand[0])
            field[y][x] = k
            used[k] = True
            if(rekurzija(step+1)):
                return True
            
            used[k] = False
        return False
        
    else:
        return True
    
rekurzija(1)

for y in range(3):
    l,c,r = (koscki[field[y][0]], koscki[field[y][1]], koscki[field[y][2]])
    print(" %s  %s  %s " % (l.sides[1], c.sides[1], r.sides[1]))
    print("%s %s%s %s%s %s" % (l.sides[0], l.sides[2], c.sides[0],c.sides[2], r.sides[0],r.sides[2]))
    print(" %s  %s  %s " % (l.sides[3], c.sides[3], r.sides[3]))