Algorithm 如何改进交通堵塞递归解算器的算法?
Android上有一个可爱的小游戏叫做 我编写了一个递归解算器:Algorithm 如何改进交通堵塞递归解算器的算法?,algorithm,recursion,graph-theory,Algorithm,Recursion,Graph Theory,Android上有一个可爱的小游戏叫做 我编写了一个递归解算器: import copy,sys sys.setrecursionlimit(10000) def lookup_car(car_string,ingrid): car=[] i=0 for row in ingrid: j=0 for cell in row: if cell == car_string: car.append([i,j]) j+=1
import copy,sys
sys.setrecursionlimit(10000)
def lookup_car(car_string,ingrid):
car=[]
i=0
for row in ingrid:
j=0
for cell in row:
if cell == car_string:
car.append([i,j])
j+=1
i+=1
return car
#True if up/down False if side to side
def isDirectionUp(car):
init_row=car[0][0]
for node in car:
if node[0] != init_row:
return True
return False
def space_up(car,grid):
top_node=car[0]
m_space_up = (top_node[0]-1,top_node[1])
if top_node[0] == 0:
return -1
elif grid[m_space_up[0]][m_space_up[1]] != " ":
return -1
else:
return m_space_up
def space_down(car,grid):
bottom_node = car[-1]
m_space_down = (bottom_node[0]+1,bottom_node[1])
if bottom_node[0] == 5 :
return -1
elif grid[m_space_down[0]][m_space_down[1]] != " ":
return -1
else:
return m_space_down
def space_left(car,grid):
left_node = car[0]
m_space_left = (left_node[0],left_node[1]-1)
if left_node[1] == 0 :
return -1
elif grid[m_space_left[0]][m_space_left[1]] != " ":
return -1
else:
return m_space_left
def space_right(car,grid):
right_node = car[-1]
m_space_right = (right_node[0],right_node[1]+1)
if right_node[1] == 5 :
return -1
elif grid[m_space_right[0]][m_space_right[1]] != " ":
return -1
else:
return m_space_right
def list_moves(car,grid):
ret =[]
if isDirectionUp(car):
up = space_up(car,grid)
if up != -1:
ret.append(("UP",up))
down = space_down(car,grid)
if down != -1:
ret.append(("DOWN",down))
else:
left = space_left(car,grid)
if left != -1:
ret.append(("LEFT",left))
right = space_right(car,grid)
if right != -1:
ret.append(("RIGHT",right))
return ret
def move_car(car_string,move,ingrid):
grid = copy.deepcopy(ingrid)
car = lookup_car(car_string,grid)
move_to = move[1]
front = car[0]
back = car[-1]
if(move[0] == "UP" or move[0] == "LEFT"):
grid[back[0]][back[1]] = " "
grid[move_to[0]][move_to[1]] = car_string
elif(move[0] == "DOWN" or move[0] == "RIGHT"):
grid[front[0]][front[1]] = " "
grid[move_to[0]][move_to[1]] = car_string
return grid
def is_solution(grid):
car = lookup_car("z",grid)
if(car[-1] == [2,5]):
return True
elif space_right(car,grid) == -1:
return False
else:
solgrid = move_car("z",("RIGHT",space_right(car,grid)),grid)
return is_solution(solgrid)
def print_grid(grid):
for row in grid:
print ''.join(row)
def solve(grid,solution,depth):
global stop
global state
if grid in state:
return
else:
state.append(grid)
if stop:
return
if is_solution(grid):
print_grid(grid)
print len(solution)
else:
for each in "abcdefzhijklm":
car = lookup_car(each,grid)
moves = list_moves(car,grid)
for move in moves:
solution.append((each,move))
moved_grid = move_car(each,move,grid)
solve(moved_grid,solution,depth)
stop=False
state=[]
recdepth=0
#grid file using a-w and x means free space, and ' ' means yellow car
grid=[list(x) for x in file(sys.argv[1]).read().split('\n')[0:-1]]
solve(grid,[],0)
网格在文件中的位置:
abccdd
abeef
azzhfi
jjjhfi
kll
kmm
但是,要找到一个解决方案需要8000多个动作,而找不到一个简单的30个动作的解决方案。算法有什么问题?如果搜索空间的分支因子是r,那么搜索树中深度n的顶点数是(1-r^n)/(1-r)。即使在r=2的简单情况下,一个具有最小30步解决方案的问题也会有一个具有大约2^30-1~=100000000个顶点的搜索树。现在,您的分支因子很可能大于2,因此30步的问题离简单问题还有很长的路要走 <>你说,我倾向于(a)找到一个更好的表示你的问题(搜索字符串的数组是缓慢的),(b)考虑最佳的第一次搜索,在那里你用启发式方法指导你的搜索(例如,黄色的汽车从它的目的地或阻塞黄色小路的小汽车的数量)。 希望这能有所帮助。这本质上是一个(相对而言)搜索问题,搜索空间很大。正如其他人所建议的那样,继续阅读,然后继续阅读,当你了解到差异时,继续阅读,并提出一个悲观的评分函数 也请注意,在这种情况下,您已经知道结束状态应该是什么,因此,另一种方法是从两端搜索并在中间相遇。这将大大减少您的搜索空间
如果这还不够,你可以结合这些技术是什么阻止了它进入循环?也考虑了不同之处,看看什么最适合这个问题。问题在于你的SOLVER()函数。您试图实现什么样的搜索算法?假设是BFS,看看维基百科关于“广度优先搜索”的文章,并尝试更忠实地将其伪代码翻译成python.Lou。阻止它进入循环的是保存历史的全局状态变量。然后停止递归,如果它已经遇到这种状态我可以诅咒你吗?A:让我下载游戏,B:让我想为它写一个解算器?一个建议是:为什么不使用实际的空格而不是x来表示空的空格呢。初始化集合序列∑,以包含一个包含所有顶点的集合。将顶点的输出序列初始化为空。当∑为非空时:从∑中的第一个集合中查找并移除顶点v如果∑中的第一个集合现在为空,则将其从∑中移除并将v添加到输出序列的末尾。对于每个边v-w,使得w仍然属于∑中的集S:如果在处理v时包含w的集S尚未被替换,则创建一个新的空替换集T,并将其放置在序列中的S之前;否则,让T成为S之前的集合。将w从S移动到T,如果这导致S变为空,则从序列中移除S。行动小组,我将剩下的留给你。很多感兴趣的人。有人想出他们自己的解决方案了吗?