Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. class Queue:
  2. def __init__(self):
  3. self._queue = []
  4. self._count = 0
  5. self._first_element = 0
  6.  
  7. def enqueue(self, value):
  8. self._queue.append(value)
  9. self._count += 1
  10.  
  11. def dequeue(self):
  12. if self._count > 0:
  13. self._first_element += 1
  14. self._count -= 1
  15. else:
  16. raise Exception("Empty array")
  17.  
  18. def peek(self):
  19. if self._count > 0:
  20. return self._queue[self._first_element]
  21. else:
  22. raise Exception("Empty array")
  23.  
  24. def empty(self):
  25. return self._count == 0
  26.  
  27.  
  28. def reverse_maze(maze):
  29. new_maze = []
  30. for i in range(len(maze) - 1, -1, -1):
  31. temp_array = []
  32. for j in range(len(maze[i]) - 1, -1, -1):
  33. temp_array.append(maze[i][j])
  34. new_maze.append(temp_array)
  35. return new_maze
  36.  
  37.  
  38. directions = [
  39. [+1, 0], # Down
  40. [0, +1], # Right
  41. [-1, 0], # Up
  42. [0, -1] # Left
  43. ]
  44.  
  45.  
  46. def create_matrix(map):
  47. matrix = []
  48. for i in range(len(map)):
  49. matrix.append([-1] * len(map[i]))
  50. return matrix
  51.  
  52.  
  53. def solve(map, miner, exit):
  54. map = reverse_maze(map)
  55.  
  56. if len(map[0]) > 1:
  57. miner['x'], miner['y'] = miner['y'], miner['x']
  58. exit['x'], exit['y'] = exit['y'], exit['x']
  59.  
  60. check = create_matrix(map)
  61.  
  62. queue = Queue()
  63. queue.enqueue([exit["x"], exit["y"]])
  64. print(exit['x'])
  65. print(exit['y'])
  66. check[exit["x"]][exit["y"]] = 0
  67.  
  68. while not queue.empty():
  69. x = queue.peek()[0]
  70. y = queue.peek()[1]
  71.  
  72. for i in range(4):
  73. new_x = x + directions[i][0]
  74. new_y = y + directions[i][1]
  75.  
  76. if 0 <= new_x < len(map) and 0 <= new_y < len(map[0]) and map[new_x][new_y] and check[new_x][new_y] == -1:
  77. queue.enqueue([new_x, new_y])
  78. check[new_x][new_y] = check[x][y] + 1
  79.  
  80. queue.dequeue()
  81.  
  82. coordenates = [miner['x'], miner['y']]
  83. answer = []
  84. min_value = check[coordenates[0]][coordenates[1]]
  85. while min_value != 0:
  86. temp_mov = ''
  87. new_x = 0
  88. new_y = 0
  89. for i in range(4):
  90. x = coordenates[0] + directions[i][0]
  91. y = coordenates[1] + directions[i][1]
  92.  
  93. if 0 <= x < len(map) and 0 <= y < len(map[0]) and check[x][y] != -1 and check[x][y] < min_value:
  94. min_value = check[x][y]
  95. if i == 0:
  96. if len(map[0]) == 1:
  97. temp_mov = "right"
  98. else:
  99. temp_mov = "down"
  100. elif i == 1:
  101. temp_mov = "right"
  102. elif i == 2:
  103. if len(map[0]) == 1:
  104. temp_mov = "left"
  105. else:
  106. temp_mov = "up"
  107. else:
  108. temp_mov = "left"
  109. new_x = x
  110. new_y = y
  111.  
  112. answer.append(temp_mov)
  113. coordenates = [new_x, new_y]
  114.  
  115. return answer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement