Advertisement
Guest User

hanoi python

a guest
Nov 21st, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. def hanoi(n, origem, ajuda, destino):
  4.     print "hanoi( ", n, origem, ajuda, destino, " e lá vamos nós"
  5.     if n > 0:
  6.         # mova a torre de tamanho n - 1 para ajuda:
  7.         hanoi(n - 1, origem, destino, ajuda)
  8.         # mova o disco do bastão origem para o bastão destino
  9.         if origem[0]:
  10.             disco = origem[0].pop()
  11.             print "movendo o disco " + str(disco) + " da " + origem[1] + " para " + destino[1]
  12.             destino[0].append(disco)
  13.         # mova a torre de tamanho n-1 da ajuda para o destino
  14.         hanoi(n - 1, ajuda, origem, destino)
  15.        
  16. origem = ([4,3,2,1], "origem")
  17. destino = ([], "destino")
  18. ajuda = ([], "ajuda")
  19. hanoi(len(origem[0]),origem,ajuda,destino)
  20.  
  21. print origem, ajuda, destino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement