星鳕鱼问题

2024-09-27 23:17:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,使我的明星实施工作。你能指路吗?谢谢您。在

#!/usr/bin/env python

import sys, re, math  

grid = []  
g = []
h = []

width =  int(sys.argv[1])
height = int(sys.argv[2])

open = []
close = []

startpos = (0,0) #(height,width)
endpos = (6,5) #(height,width)

#Functions

def findlowestcostinopen():
 lowest = 9999
 lowestpair = []
 for q in open:
  sum = int(g[q[0]][q[1]])+int(h[q[0]][q[1]])
  #print sum,lowest
  if sum<lowest:
   lowestpair = q
   lowest=sum

 return lowestpair
# Init

for q in range(height):
 temp = []
 for w in range(width):
  temp.append((0,0))
 grid.append(temp)

for q in range(height):
 temp = []
 for w in range(width):
  temp.append(0)
 g.append(temp)

for q in range(height):  
 temp = []  
 for w in range(width):    
  temp.append(0)  
 h.append(temp)  



for q in range(height):  
 for w in range(width):  
  h[q][w]=abs(endpos[0]-q)*10 + abs(endpos[1]-w)*10  

open.append(startpos)  
switch = True  
while switch and open:  
 #Find the smallest cost  
 lowestcost = findlowestcostinopen()  
 print lowestcost,endpos  
 if lowestcost == endpos:  
  switch = False  
  print 'found',lowestcost  


 parentgcost=int(g[lowestcost[0]][lowestcost[1]])    
 #print parentgcost  
 #Check every directly connected node     
 for q in range(-1,2):    
  for w in range(-1,2):    
   currentnode = ((lowestcost[0]+q),(lowestcost[1]+w))  
   if q==0 and w==0:  
    ''''''
   elif(currentnode[0]<0 or currentnode[0]>(height-1)):  
    '''Vertical out'''  
   elif(currentnode[1]<0 or currentnode[1]>(width-1)):  
    '''Horizontal out'''  
   elif(grid[currentnode[0]][currentnode[1]]=='wall'):
    '''WALL'''  
   elif open.count((currentnode[0],currentnode[1]))>0: 

    ''''''  
    currentg = g[currentnode[0]][currentnode[1]]    

    if (q==0 and w==1) or (q==0 and w==-1) or (q==1 and w==0) or (q==-1 and w==0):  
     newsum = parentgcost+10  
    else: newsum = parentgcost+14  

    if newsum<currentg:  
     g[currentnode[0]][currentnode[1]]=newsum  

    grid[currentnode[0]][currentnode[1]]=lowestcost   

   elif close.count((currentnode[0],currentnode[1]))>0:  
    '''EXISTS IN CLOSE'''  
   else:   
    #Time to calculate g values  
    if q==0:
     if w==-1 or w==1:
      nodecost = parentgcost+10
    elif q==1:  
     if w==0:  
      nodecost = parentgcost+10  
     else:   
      nodecost = parentgcost+14  
    elif q==-1:  
     if w==0:  
      nodecost = parentgcost+10  
     else:  
      nodecost = parentgcost+14  
    g[(currentnode[0])][(currentnode[1])]=nodecost   
    grid[(currentnode[0])][(currentnode[1])]=lowestcost  
    #print nodecost  

    open.append(currentnode)  

Tags: andinforifrangeopenwidthtemp
2条回答

这是一个*搜索的伪代码。它很容易被转录成Python。在

Wikipedia A* search page

一些问题:

  1. 您这样做是为了征求意见:

    '''some text'''
    

    这实际上不是一个注释,而是一个字符串。你只是不把它分配给任何东西。请改为发表评论:

    # some text
    
  2. 这段代码很难阅读:

            if q==0:
             if w==-1 or w==1:
              nodecost = parentgcost+10
            elif q==1:  
             if w==0:  
              nodecost = parentgcost+10  
             else:   
              nodecost = parentgcost+14  
            elif q==-1:  
             if w==0:  
              nodecost = parentgcost+10  
             else:  
              nodecost = parentgcost+14  
    

    更改为:

            if q==0 and (w==-1 or w==1):
                nodecost = parentgcost+10
            elif q==1 and w==0:  
                nodecost = parentgcost+10  
            elif q==1:  
                nodecost = parentgcost+14  
            elif q==-1 and w==0:  
                nodecost = parentgcost+10  
            elif q==-1:  
                nodecost = parentgcost+14  
    

    请注意如何使用四个空格来缩进,而不仅仅是一个。

  3. 此处不需要括号:

            g[(currentnode[0])][(currentnode[1])]=nodecost   
    

    更改为

            g[currentnode[0]][currentnode[1]]=nodecost   
    
  4. 你太喜欢索引了。这也让人难以阅读。在

            g[(currentnode[0])][(currentnode[1])]=nodecost
    

    最好是

            height, width = currentnode
            g[height][width] = nodecost
    

这些都不能解决您的问题,因为您没有说明这是什么,甚至代码应该做什么。在

相关问题 更多 >

    热门问题