2016-05-24 87 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 

這應該給你一個良好的開端實現實際的算法。