给定一个宽度不同的路径网格,如何找到一条通向终点的路径?
路径将由一个二维数组表示,其中0表示不能行走,1表示它是可步行的,2表示起点,3表示终点。请考虑以下示例:
21111111100000
00000011000000
00001111111111
00001111100111
00001110000101
00001111100113
在上面的例子中,路径的宽度从1到3变化,并且存在许多将导致终点的解决方案。我想找到一条通向它的路径,路径不一定是最短路径(也不应该是最长路径)。每条路径的宽度都是未知的,这意味着除了起点和终点之外,网格可以是全部的#1; 1" s。
编辑:路径不应包含不必要的"浪费"步行意味着如果垂直路径的宽度为2,结果不应该沿着路径向下走,然后向右走一步然后一直向上走
答案 0 :(得分:0)
我同意Calumn:DFS是最简单的方法。这是一个类似python的伪代码的简单解决方案。它将打印解决方案作为一系列的' R',' D' D'表示左,右,上或下。
def flood(x,y,story):
if (visited[x][y] or map[x][y]=='0'): return;
visited[x][y]=True;
if (map[x][y]=='3'):
print 'done. The path is: '+story
return
if (x<len(a[0])): flood(x+1,y,story+'R')
if (y<len(a)): flood(x,y+1,story+'D')
if (x>0): flood(x-1,y,story+'L')
if (y>0): flood(x,y-1,story+'U')
def solve(map):
visited = array_of_false_of_same_size_as(map)
x,y = find_the_two(map)
flood(x,y,'')
一旦找到解决方案就使其停止的优化留给读者一个练习(你可以使洪水返回一个布尔值来指示它是否找到了什么,或使用全局标志)。
(p.s。我做了这个回答社区维基,因为我只是澄清了Calumn的回答。我不能说得很多信用)
对于它的价值,并且只是为了表明广度优先搜索并不复杂,Python中的实际可运行程序:
def find(grid, xstart=0, ystart=0):
# Maps (xi,yi) to (x(i-1), y(i-1))
prev = {(xstart, ystart):None}
# Prepare for the breadth-first search
queue = [(xstart, ystart)]
qpos = 0
# Possibly enqueue a trial coordinate
def enqueue(prevxy, dx, dy):
x = prevxy[0] + dx
y = prevxy[1] + dy
xy = (x, y)
# Check that it hasn't been visited and the coordinates
# are valid and the grid position is not a 0
if (xy not in prev
and x >= 0 and x < len(grid)
and y >= 0 and y < len(grid[x])
and grid[x][y] != 0):
# Record the history (and the fact that we've been here)
prev[xy] = prevxy
# If we found the target, signal success
if grid[x][y] == 3:
return xy
# Otherwise, queue the new coordinates
else:
queue.append(xy)
return None
# The actual breadth-first search
while qpos < len(queue):
xy = queue[qpos]
qpos += 1
found = ( enqueue(xy, 1, 0)
or enqueue(xy, 0, 1)
or enqueue(xy, -1, 0)
or enqueue(xy, 0, -1))
if found: break
# Recover the path
path = []
while found:
path.append(found)
found = prev[found]
path.reverse()
return path
# Test run
grid = [ [2,1,1,1,1,1,1,1,1,0,0,0,0,0]
, [0,0,0,0,0,0,1,1,0,0,0,0,0,0]
, [0,0,0,0,1,1,1,1,1,1,1,1,1,1]
, [0,0,0,0,1,1,1,1,1,0,0,1,1,1]
, [0,0,0,0,1,1,1,0,0,0,0,1,0,1]
, [0,0,0,0,1,1,1,1,1,0,0,1,1,3]
]
for x, y in find(grid): grid[x][y]='*'
print '\n'.join(''.join(str(p) for p in line) for line in grid)
输出:
*******1100000
000000*1000000
000011******11
00001111100*11
00001110000*01
00001111100***