2016-05-24 88 views
-3

我想实现一个A * algorithm in Python。该代理最初位于坐标(6,2)处,正试图到达坐标(4,11)。此代理的Universe是12x12 positions的网格,其中一部分职位无法访问。代理人的行为非常基本:它可以在其当前位置的北部,南部,东部和西部移动一步。对角线移动是不允许的。代理人不知道其立场。 无法访问是和是只有当紧邻这个锁定位置。 从初始位置计数的路径上由代理给定为行驶距离(距离 到目前为止)每个帕索在Python中实现A *算法

1 I有许多困难建立在这种情况下将是12x12网格位置搜索宇宙。

2不知道定义坐标剂移动,考虑到不可达

感谢您的帮助块

回答

1

你可以设计你的网格,布尔值,如清单列表:

grid = [[True for i in range(12)] for i in range(12)] 

和设置不能访问False

not_accessible = [(5, 10), (10, 7), (1, 0), (2, 4), (5, 2), 
        (5, 8), (5, 2), (8, 7), (6, 11), (2, 9), 
        (11, 0), (2, 10), (3, 4), (3, 5), (1, 5), 
        (8, 1), (3, 1), (11, 11), (9, 3), (3, 7)] 

for x, y in not_accessible: 
    grid[x][y] = False 
位置10

,并定义了一些功能,移动

def new_field(direction, x0, y0, grid): 
    if direction == 'L': 
     x1, y1 = x0 - 1, y0 
    elif direction == 'R': 
     x1, y1 = x0 + 1, y0 
    elif direction == 'U': 
     x1, y1 = x0, y0 + 1 
    elif direction == 'D': 
     x1, y1 = x0, y0 - 1 

    if x1 not in range(12) or y1 not in range(12): 
     raise Exception('New field outside of grid') 
    if not grid[x1][y1]: 
     raise Exception('New field not accessible') 

    return x1, y1 

例如

print new_field('U', 0, 0, grid) 
# => (0, 1) 

print new_field('D', 0, 0, grid) 
# => Exception: New field outside of grid 

print new_field('R', 0, 0, grid) 
# => Exception: New field not accessible 

这应该给你一个良好的开端实现实际的算法。