|
| 1 | +import heapq |
| 2 | +import random |
| 3 | + |
| 4 | +class MazeSolver: |
| 5 | + def __init__(self, maze): |
| 6 | + self.maze = maze |
| 7 | + self.rows = len(maze) |
| 8 | + self.cols = len(maze[0]) |
| 9 | + self.start = (0, 0) |
| 10 | + self.goal = (self.rows - 1, self.cols - 1) |
| 11 | + |
| 12 | + def get_neighbors(self, node): |
| 13 | + row, col = node |
| 14 | + neighbors = [] |
| 15 | + if row > 0 and not self.maze[row - 1][col]: |
| 16 | + neighbors.append((row - 1, col)) |
| 17 | + if row < self.rows - 1 and not self.maze[row + 1][col]: |
| 18 | + neighbors.append((row + 1, col)) |
| 19 | + if col > 0 and not self.maze[row][col - 1]: |
| 20 | + neighbors.append((row, col - 1)) |
| 21 | + if col < self.cols - 1 and not self.maze[row][col + 1]: |
| 22 | + neighbors.append((row, col + 1)) |
| 23 | + return neighbors |
| 24 | + |
| 25 | + def heuristic(self, node): |
| 26 | + return abs(node[0] - self.goal[0]) + abs(node[1] - self.goal[1]) |
| 27 | + |
| 28 | + def solve(self): |
| 29 | + open_set = [] |
| 30 | + closed_set = set() |
| 31 | + g_score = {self.start: 0} |
| 32 | + f_score = {self.start: self.heuristic(self.start)} |
| 33 | + heapq.heappush(open_set, (f_score[self.start], self.start)) |
| 34 | + |
| 35 | + while open_set: |
| 36 | + _, current = heapq.heappop(open_set) |
| 37 | + |
| 38 | + if current == self.goal: |
| 39 | + path = [] |
| 40 | + while current in g_score: |
| 41 | + path.append(current) |
| 42 | + current = g_score[current] |
| 43 | + return path[::-1] |
| 44 | + |
| 45 | + closed_set.add(current) |
| 46 | + |
| 47 | + for neighbor in self.get_neighbors(current): |
| 48 | + if neighbor in closed_set: |
| 49 | + continue |
| 50 | + tentative_g_score = g_score[current] + 1 |
| 51 | + |
| 52 | + if neighbor not in g_score or tentative_g_score < g_score[neighbor]: |
| 53 | + g_score[neighbor] = tentative_g_score |
| 54 | + f_score[neighbor] = tentative_g_score + self.heuristic(neighbor) |
| 55 | + heapq.heappush(open_set, (f_score[neighbor], neighbor)) |
| 56 | + |
| 57 | + return None |
| 58 | + |
| 59 | +def generate_random_maze(rows, cols, obstacle_probability): |
| 60 | + return [[random.random() < obstacle_probability for _ in range(cols)] for _ in range(rows)] |
| 61 | + |
| 62 | +def print_maze(maze): |
| 63 | + for row in maze: |
| 64 | + print("".join(["#" if cell else " " for cell in row])) |
| 65 | + |
| 66 | +def main(): |
| 67 | + rows = 10 |
| 68 | + cols = 10 |
| 69 | + obstacle_probability = 0.2 |
| 70 | + |
| 71 | + maze = generate_random_maze(rows, cols, obstacle_probability) |
| 72 | + print("Random Maze:") |
| 73 | + print_maze(maze) |
| 74 | + |
| 75 | + maze_solver = MazeSolver(maze) |
| 76 | + path = maze_solver.solve() |
| 77 | + |
| 78 | + if path: |
| 79 | + print("\nSolution Path:") |
| 80 | + for node in path: |
| 81 | + maze[node[0]][node[1]] = True # Marking the path in the maze |
| 82 | + print_maze(maze) |
| 83 | + else: |
| 84 | + print("\nNo path found!") |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments