你可以設計你的網格,布爾值,如清單列表:
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
這應該給你一個良好的開端實現實際的算法。