Advertisement
Guest User

Labirynt

a guest
Jul 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. from collections import deque
  2. from termcolor import colored
  3. import os
  4. plik = open("maze.txt")
  5. MAZE = []
  6.  
  7.  
  8. def print_maze():
  9.     os.system('clear') # system("cls")
  10.     for row in MAZE:
  11.         for x in row:
  12.             if x == '#':
  13.                 print(colored(x,'blue'),end=' ')
  14.             elif x == '+':
  15.                 print(colored(x,'red'),end=' ')
  16.             else:
  17.                 print(colored(x,'green'), end=' ')
  18.         print()
  19.  
  20.  # Wczytywanie labiryntu
  21. for line in plik:
  22.     MAZE.append(list(line[:-1]))
  23.  
  24. n, m = len(MAZE), len(MAZE[0])
  25. start, meta = None, None
  26. for i in range(n):
  27.     for j in range(m):
  28.         if MAZE[i][j] == 'S':
  29.             start = (i, j)
  30.         if MAZE[i][j] == 'G':
  31.             meta = (i, j)
  32.  
  33.  
  34. Q = deque()
  35. odwiedzone = set()
  36. skad_przyszedlem = {}
  37.  
  38. Q.append(start)
  39. odwiedzone.add(start)
  40. MAZE[start[0]][start[1]] = '.'
  41.  
  42. dx = [0, 0, 1, -1]
  43. dy = [1, -1, 0, 0]
  44. while len(Q) > 0:
  45.     act = Q.popleft()
  46.     print_maze()
  47.     if act == meta:
  48.         print('META')
  49.         break
  50.     for i in range(4):
  51.         new_x = act[0] + dx[i]
  52.         new_y = act[1] + dy[i]
  53.         new_move = (new_x, new_y)
  54.         GOALS = [' ', 'G']
  55.         if MAZE[new_x][new_y] in GOALS:
  56.             # odwiedzone.add(new_move)
  57.             Q.append(new_move)
  58.             MAZE[new_x][new_y] = '.'
  59.             skad_przyszedlem[ new_move ] = act
  60.  
  61.  
  62. while meta != start:
  63.     meta = skad_przyszedlem[ meta ]
  64.     MAZE[ meta[0] ][ meta[1] ] = '+'
  65. os.system('clear') # system("cls")
  66. print_maze()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement