Advertisement
fmasanori

Labirinto.py

Nov 19th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. labirinto = '''
  2. +-----+-----+-+
  3. |     |     |E|
  4. | | +-+  -+ | |
  5. | | |     | | |
  6. | |   +---+ | |
  7. | | | |     | S
  8. |   | | | | | |
  9. |-----+ +-+-+ |
  10. |     | | |   |
  11. | +-+   | |  -|
  12. | |-+-|   |-  |
  13. | | | +---+-- |
  14. |             |
  15. +-------------+
  16. '''
  17.  
  18. ENTRADA, LIVRE, PASSEI, SOLUÇÃO, SAÍDA = 'E .oS'
  19.  
  20. def saiu(x, y):
  21.     if lab[x][y] == SAÍDA:
  22.         return True
  23.     if lab[x][y] in (ENTRADA, LIVRE):
  24.         lab[x][y] = PASSEI
  25.         if saiu(x-1, y) or saiu(x+1, y) or 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 = []
  32. for x in labirinto.splitlines():
  33.   if len(x) > 0:
  34.     x = list(x)
  35.     lab.append(x)
  36.  
  37. for j in range(len(lab)):
  38.   for k in range(len(lab[j])):
  39.     if lab[j][k] == ENTRADA:
  40.       x = j
  41.       y = k
  42.       break
  43.  
  44. if saiu(x, y):
  45.     for linha in lab:
  46.         print (''.join(linha))
  47. else:
  48.     print ('A formiga irá morrer...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement