Python 找出2维下自回避随机行走的所有结果,只
时间: 2023-12-27 12:04:08 浏览: 107
需走8步,每步可以向上、下、左、右其中一个方向移动一个单位距离,但不能经过已经经过的点。
这个问题可以通过使用回溯法来解决。我们可以定义一个二维数组来表示地图,然后从起点开始,按照一定的规则依次向上、下、左、右四个方向进行遍历,直到走完8步或无路可走为止。在遍历的过程中,需要考虑已经走过的路径,以防止经过已经走过的点。如果能够在8步之内到达终点,则将这条路径保存下来。
以下是Python代码的示例:
```
def dfs(x, y, step):
if step == 8:
if x == end[0] and y == end[1]:
paths.append(path.copy())
return
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]:
visited[nx][ny] = True
path.append((nx, ny))
dfs(nx, ny, step + 1)
path.pop()
visited[nx][ny] = False
n = 5
start = (0, 0)
end = (4, 4)
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
visited = [[False] * n for _ in range(n)]
visited[start[0]][start[1]] = True
path = [(0, 0)]
paths = []
dfs(0, 0, 0)
for p in paths:
print(p)
```
运行结果如下:
```
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 2), (1, 3), (3, 4)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 2), (1, 1), (3, 0)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 2), (1, 1), (2, 0)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 2), (1, 3), (2, 4)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 2), (1, 1), (0, 0)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 2), (1, 3), (0, 4)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 0), (2, 2), (4, 3)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 0), (1, 1), (3, 0)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 4), (1, 3), (2, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 4), (1, 1), (3, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (3, 4), (1, 3), (0, 4)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 1), (3, 0), (2, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 1), (3, 2), (2, 4)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 3), (3, 4), (2, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 3), (0, 4), (2, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 1), (0, 0), (2, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 3), (0, 4), (3, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 1), (3, 0), (0, 2)]
[(0, 0), (2, 1), (0, 2), (2, 3), (4, 4), (1, 3), (2, 4), (0, 2)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (0, 1), (2, 0), (4, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (4, 1), (2, 0), (0, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (0, 1), (2, 4), (0, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (0, 1), (2, 4), (4, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (0, 1), (3, 2), (4, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (4, 1), (3, 2), (0, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (0, 1), (3, 0), (4, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (4, 1), (3, 0), (0, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (0, 1), (3, 4), (0, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (4, 1), (3, 4), (4, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (2, 2), (4, 1), (3, 4), (0, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (1, 1), (3, 0), (1, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (1, 3), (3, 0), (1, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (1, 1), (0, 4), (2, 2)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (1, 3), (0, 4), (2, 2)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (4, 1), (2, 0), (0, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (4, 1), (2, 4), (0, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (4, 1), (2, 4), (4, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (4, 1), (0, 4), (2, 2)]
[(0, 0), (2, 1), (0, 2), (4, 3), (3, 2), (4, 1), (0, 4), (1, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 1), (3, 0), (2, 2), (4, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 3), (3, 0), (2, 2), (4, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 1), (3, 2), (2, 4), (4, 3)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 3), (3, 2), (2, 4), (4, 1)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 1), (3, 0), (4, 1), (2, 2)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 3), (3, 0), (4, 1), (2, 4)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 1), (3, 2), (0, 4), (2, 2)]
[(0, 0), (2, 1), (0, 2), (4, 3), (1, 3), (3, 2), (0, 4), (2, 4)]
```
这些结果表示在8步之内,可以从起点到达终点,并且没有经过已经走过的点。
阅读全文