问题与明星代码

问题描述:

我有一个问题让我的明星实施工作。你可以指路吗?谢谢。问题与明星代码

#!/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) 
+0

请问您能否正确格式化该代码?否则,你将得不到答案。 – hyperboreean 2011-01-11 09:57:20

一些问题:

  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 
    

无的,这将解决您的问题,因为你没有说那是什么,甚至什么代码是应该做的。

这是A *搜索的伪代码。它很容易转录成Python。

Wikipedia A* search page