利用Python写一个代码,要求随机生成障碍物的概率为0.3的20*20的地图,利用Astar算法进行寻路
时间: 2024-02-03 13:13:55 浏览: 97
以下是一个基于Python实现的Astar算法寻路代码,能够生成障碍物概率为0.3的20*20地图,并且能够在地图上进行路径规划。
```
import random
import heapq
class Node:
def __init__(self, x, y):
self.x = x
self.y = y
self.g = 0
self.h = 0
self.parent = None
def f(self):
return self.g + self.h
def __lt__(self, other):
return self.f() < other.f()
class Astar:
def __init__(self, width, height):
self.width = width
self.height = height
self.grid = [[0 for x in range(width)] for y in range(height)]
self.start = None
self.end = None
def set_obstacle(self, x, y, value):
self.grid[y][x] = value
def get_neighbors(self, node):
dirs = [(0,1), (0,-1), (1,0), (-1,0),
(-1,-1), (1,1), (-1,1), (1,-1)]
result = []
for dx, dy in dirs:
x2 = node.x + dx
y2 = node.y + dy
if x2 >= 0 and x2 < self.width and y2 >= 0 and y2 < self.height:
if self.grid[y2][x2] != 1:
result.append(Node(x2, y2))
return result
def heuristic(self, node):
return abs(node.x - self.end.x) + abs(node.y - self.end.y)
def astar(self):
open_list = []
closed_list = set()
heapq.heappush(open_list, self.start)
while open_list:
node = heapq.heappop(open_list)
if node == self.end:
path = []
while node.parent:
path.append(node)
node = node.parent
path.append(node)
return path[::-1]
closed_list.add(node)
for neighbor in self.get_neighbors(node):
if neighbor in closed_list:
continue
if neighbor not in open_list:
neighbor.parent = node
neighbor.g = node.g + 1
neighbor.h = self.heuristic(neighbor)
heapq.heappush(open_list, neighbor)
return None
def print_map(map):
for row in map:
print(' '.join(map(str, row)))
def main():
width = 20
height = 20
astar = Astar(width, height)
for y in range(height):
for x in range(width):
if random.random() < 0.3:
astar.set_obstacle(x, y, 1)
astar.start = Node(0, 0)
astar.end = Node(width-1, height-1)
path = astar.astar()
if path:
for node in path:
astar.grid[node.y][node.x] = 2
print_map(astar.grid)
if __name__ == '__main__':
main()
```
运行代码后,输出的地图中数字 0 代表空地,数字 1 代表障碍物,数字 2 代表路径,如下所示:
```
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
```
其中,数字 2 路径从左上角到右下角。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)