Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- from termcolor import colored
- import os
- plik = open("maze.txt")
- MAZE = []
- def print_maze():
- os.system('clear') # system("cls")
- for row in MAZE:
- for x in row:
- if x == '#':
- print(colored(x,'blue'),end=' ')
- elif x == '+':
- print(colored(x,'red'),end=' ')
- else:
- print(colored(x,'green'), end=' ')
- print()
- # Wczytywanie labiryntu
- for line in plik:
- MAZE.append(list(line[:-1]))
- n, m = len(MAZE), len(MAZE[0])
- start, meta = None, None
- for i in range(n):
- for j in range(m):
- if MAZE[i][j] == 'S':
- start = (i, j)
- if MAZE[i][j] == 'G':
- meta = (i, j)
- Q = deque()
- odwiedzone = set()
- skad_przyszedlem = {}
- Q.append(start)
- odwiedzone.add(start)
- MAZE[start[0]][start[1]] = '.'
- dx = [0, 0, 1, -1]
- dy = [1, -1, 0, 0]
- while len(Q) > 0:
- act = Q.popleft()
- print_maze()
- if act == meta:
- print('META')
- break
- for i in range(4):
- new_x = act[0] + dx[i]
- new_y = act[1] + dy[i]
- new_move = (new_x, new_y)
- GOALS = [' ', 'G']
- if MAZE[new_x][new_y] in GOALS:
- # odwiedzone.add(new_move)
- Q.append(new_move)
- MAZE[new_x][new_y] = '.'
- skad_przyszedlem[ new_move ] = act
- while meta != start:
- meta = skad_przyszedlem[ meta ]
- MAZE[ meta[0] ][ meta[1] ] = '+'
- os.system('clear') # system("cls")
- print_maze()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement