Advertisement
daemonio

Torre de Hanoi Animada (Codigo Python)

Jul 4th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Sat Jul  4 15:09:13 BRT 2015
  3. # [hanoianimado.py]
  4. # Execucao:
  5. # $ python hanoianimado.py
  6. #
  7.  
  8. # Desenha as hastes na tela.
  9. def Desenha(hastes, base):
  10.     linhas = []
  11.     for h in hastes:
  12.         linha = []
  13.         for z in h:
  14.             if z == 0:
  15.                 e = ' ' * ((base-1)/2)
  16.                 e += '|'
  17.                 e += ' ' * ((base-1)/2)
  18.    
  19.                 if len(e) < base:
  20.                     e = e + ' ' * (base - len(e))
  21.    
  22.                 linha.append(e)
  23.             else:
  24.                 e = ' ' * ((base-z)/2)
  25.                 e += 'o' * z
  26.                 e += ' ' * ((base-z)/2)
  27.    
  28.                 if len(e) < base:
  29.                     e = e + ' ' * (base - len(e))
  30.    
  31.                 linha.append(e)
  32.         linha.append('*' * base)
  33.         linhas.append(linha)
  34.    
  35.     for z in zip(*linhas):
  36.         for s in z:
  37.             print s,
  38.         print
  39.    
  40. #
  41. # MAIN
  42. #
  43. n = int(raw_input('Digite o numero de discos: '))
  44. h = int(raw_input('Digite a haste  de origem (1, 2, 3): '))
  45.  
  46. hastes = []
  47. discos = [ 2*i for i in range(2,n+2) ]
  48. base = max(discos) + 2
  49.  
  50. # Realiza permutacao de nomes. O algoritmo original somente
  51. # considera 0 como inicial. Logo, se escolher h=2 entao
  52. # considera o novo zero a haste 2 (posicao 1).
  53. NOMES = []
  54. if h == 1:
  55.     NOMES = [0, 1, 2]
  56. elif h == 2:
  57.     NOMES = [1, 0, 2]
  58. else:
  59.     NOMES = [2, 0, 1]
  60.  
  61. # Preenche inicialmente as hastes
  62. for i in range(1, 4):
  63.     if i == h:
  64.         hastes.append([0] + discos)
  65.     else:
  66.         hastes.append([0] + [ 0 for i in range(n) ])
  67.  
  68. movimentos = 1
  69. for i in range(2**n):
  70.     # Manda desenhar
  71.     Desenha(hastes, base)
  72.  
  73.     k = i+1
  74.  
  75.     # obtem haste origem
  76.     origem = NOMES[(k&(k-1))%3] ;
  77.  
  78.     # obtem haste destino
  79.     destino = NOMES[((k|(k-1))+1)%3] ;
  80.  
  81.     print
  82.     print "Mova disco de %d para %d (%d movimentos)" % (origem+1, destino+1, movimentos) ;
  83.  
  84.     #
  85.     # Descreve o movimento
  86.     #
  87.  
  88.     (j, o) = (0, 0)
  89.     # Obtem index do elemento no topo da haste origem (em o)
  90.     for h in hastes[origem]:
  91.         # h : disco no topo
  92.         if h != 0:
  93.             o = j
  94.             break
  95.         j += 1
  96.  
  97.     d=0
  98.     j=len(hastes[destino])-1
  99.     # Obtem index do elemento no topo da haste origem (em d)
  100.     for h in reversed(hastes[destino]):
  101.         if h == 0:
  102.             d = j
  103.             break
  104.         j -= 1
  105.     if d == 0: d = len(hastes[destino])-1
  106.  
  107.     # Realiza a permutacao de discos
  108.     hastes[destino][d] = hastes[origem][o]
  109.     hastes[origem][o]  = 0
  110.  
  111.     raw_input('\nPressione ENTER...')
  112.  
  113.     movimentos += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement