2017-07-02 23 views
0

我有一個當前列表,其中包含來自原點和目的地的路徑點 這是從3x3矩陣和2,2的起始位置開始並結束0,0python-什麼是遍歷座標逆序列表的有效方法

位置網格還具有細胞未在位置traversible: 0,1 0,2 1,1

所以電網看起來是這樣的:

  ----------------- 
      |Goal| Wall| Wall | 
      ================== 
      | |Wall |  | 
      =================== 
      | |  | Start| 
      =================== 

運行的算法得到的最優路徑後,我能得到可能的方法,從2,2到0,0

PathCaptured:  ['2,2,1,2,2.0', '2,2,2,1,2.0', '2,1,1,0,5.0', '2,1,2,0,4.0', '1,0,0,0,7.0'] 

所以結構表示爲:[startposx,startposy,destx, desty,score]

我希望能夠用相反的路徑來重建點。

我已經是我創建2 for循環遍歷列表,以確保我連接兩個點,拿出

[[2,2][2,1][1,0][0,0]] 

答案是否有更簡單的方法做的問題這在python中?

與當前的代碼更新:

 first_time=True 
     for i, e in reversed(list(enumerate(self.action_list))): 

      if debug: 
       print i, e 
      if first_time: 
       startx,starty,destx,desty,_ = e.split(",") 
       ctr += 1 
       box_path[ctr] = [destx, desty] 
       ctr += 1 
       box_path[ctr] = [startx,starty] 
       #box_path.append([destx,desty]) 
       #box_path.append([startx,starty]) 
       first_time = False 

      else: 
       for j in range(len(self.action_list)): 
        dx, dy, dx_1, dy_1,_= self.action_list[j].split(",") 
        Dx,Dy = box_path[ctr] 
        if (Dx == dx_1 and Dy == dy_1): 
         box_path[ctr+1] = [dx,dy] 
         ctr += 1 
         #box_path.append([dx,dy]) 
         break 
      if debug: 
       print box_path 
+0

聽起來像是你要創建的形式'的字典{(startx的,starty):[(destx1,desty1,score1),(destx2,desty2,score2),...],...}' –

+0

我更新了我是如何做到的,但我不認爲它是有效的,因爲最終我需要將輸出作爲從開始到目標的一系列步驟進行詢問。 –

回答

0

看起來你的返回值的意義最終細胞到達一些C1的目標排序,那麼你有一堆從C2細胞與所有相鄰單元格(其中一個是C1,其餘不相關)等等。所以要落後,一旦你發現連接到你,你會保證你的路徑上的細胞(好像你只是再次重複,這是浪費):

def getStep(stepStr): 
    step = map(int,stepStr.split(',')) 
    return (step[0],step[1]),(step[2],step[3]) #Can return score if you need 

goal = (2,2) #Your example 

pos = len(self.action_list)-1 
start,stop = getStep(self.action_list[pos]) 
res = [stop] 
while start != goal: 
    pos -= 1 
    nstart,nstop = getStep(self.action_list[pos]) 
    while nstop != start: 
     pos -= 1 
     nstart,nstop = getStep(self.action_list[pos]) 
    res.append(start) 
    start = nstart 
res.append(goal) 

這正是遍歷列表一次,從頭到尾。內循環跳過不連接到下一個單元格的不相關單元。我完全忽略了這裏的錯誤檢查,比如如果路徑不好(例如永遠爲nstop!=start)。

相關問題