Advertisement
fmasanori

Labirinto Formiga

Nov 9th, 2011
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. labirinto = '''
  2. +-----+-----+-+
  3. |     |     | |
  4. | | +-+  -+ | |
  5. | | |     | | |
  6. | |E  +---+ | S
  7. | | | |     | |
  8. |   | | | | | |
  9. |-----+ +-+-+ |
  10. |     | | |   |
  11. | +-+   | |  -|
  12. | |-+-|   |-  |
  13. | | | +---+-- |
  14. |             |
  15. +-------------+
  16. '''
  17.  
  18. ENTRADA, LIVRE, VISITADO, SOLUÇÃO, SAÍDA = 'E .oS'
  19. def saiu(x, y):
  20.     if lab[x][y] == SAÍDA:
  21.         return True
  22.     elif lab[x][y] == ENTRADA or lab[x][y] == LIVRE:
  23.         lab[x][y] = VISITADO
  24.         if saiu(x-1, y) or saiu(x+1, y) or \
  25.            saiu(x, y-1) or saiu(x, y+1):
  26.             lab[x][y] = SOLUÇÃO
  27.             return True
  28.     else:
  29.         return False
  30.      
  31. lab = [list(linha)
  32.        for linha in labirinto.splitlines() if linha]
  33. x = [row.count(ENTRADA) for row in lab].index(1)
  34. y = lab[x].index(ENTRADA)
  35. if saiu(x, y):
  36.     print ('\n'.join(''.join(linha) for linha in lab))
  37. else:
  38.     print ('A formiga irá morrer...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement