예제 #1
0
def resuelveHanoiAux(n, desde, hacia, otra):
    if n > 1:
        resuelveHanoiAux(n - 1, desde, otra, hacia)
        torreHanoi.mover(desde, hacia)
        resuelveHanoiAux(n - 1, otra, hacia, desde)
    else:
        torreHanoi.mover(desde, hacia)


def resuelveHanoi(n):
    resuelveHanoiAux(n, 0, 2, 1)


n = 19  # cantidad de discos
torreHanoi = Hanoi()
for i in range(n):
    torreHanoi.poner(0, n - 1 - i)
# for i in range(n):
# 	print torreHanoi.tope(0)
# 	torreHanoi.sacar(0)
# for i in range(n):
# 	torreHanoi.poner(0,n-1-i)

resuelveHanoi(n)
for i in range(3):
    print torreHanoi.vacio(i)

for i in range(n):
    print torreHanoi.tope(2)
    torreHanoi.sacar(2)
예제 #2
0
        self.startp = Pole(start, 0, 0)
        self.workspacep = Pole(workspace, 150, 0)
        self.destinationp = Pole(destination, 300, 0)
        self.startp.showpole()
        self.workspacep.showpole()
        self.destinationp.showpole()

        for i in range(n):
            self.startp.pushdisk(
                Disk("d" + str(i), 0, i * 150, 20, (n - i) * 30))

    def move_disk(self, start, destination):
        disk = start.popdisk()
        destination.pushdisk(disk)

    def move_tower(self, n, s, d, w):
        if n == 1:
            self.move_disk(s, d)
        else:
            self.move_tower(n - 1, s, w, d)
            self.move_disk(s, d)
            self.move_tower(n - 1, w, d, s)

    def solve(self):
        self.move_tower(3, self.startp, self.destinationp, self.workspacep)


speed(0)
h = Hanoi()
h.solve()