我试图在 python 上运行这个程序,它模拟一个在太空中无意识的人,看看它是否会达到它的目标。但每次运行它时都会出现内存错误..--
init_posn=[0,0]
posn_final=[2,2]
obs = [[-1,1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1]]
# algo can be improved
def obs_det(posn_x,posn_y,obs):
for e in obs:
if e[0]==posn_x & e[1]==posn_y:
return 1
return 0
posn=[]
posn.append(init_posn)
def posn_progress(posn,posn_final,obs):
i=0
non=0
while (non==0 | (posn[i][0]==posn_final[0] & posn[i][1]==posn_final[1])):
l=posn[i][0]
m=posn[i][1]
if obs_det(l,m+1,obs) == 0:
posn.append([l,m+1])
elif obs_det(l+1,m,obs) == 0:
posn.append([l+1,m])
elif obs_det(l,m-1,obs) == 0:
posn.append([l,m-1])
elif obs_det(l-1,m,obs) == 0:
posn.append([l-1,m])
else:
non=1
i=i+1
if non==1:
return 0
else:
return posn
print posn_progress(posn,posn_final,obs)