每个图形成员在深度优先搜索时包含2个符号python

问题描述 投票:1回答:1

我有这个python深度优先搜索代码,只是工作正常。

def findworld(x): 
    znext = 'z'  # dummy value for  next
    c = 0

    for c in range(len(world)): # pairs of links from (f) to  destination (t)
        f = world[c][0]
        t = world[c][1]           
        if(x == world[c][0]) : znext = world[c][1]; break
    return znext;

world = [['a', 'b'], ['b', 'c'],
         ['c', 'd'], ['d', 'i'],
         ['i', 'n'], ['n', 'm'],
         ['m', 'l'], ['l', 'k'] ,
         ['k', 'p'] , ['p', 'u'], ['u', 'v'] , ['v', 'w'] , ['w', 'x'] , ['x', 'y'] ] # this is a set of linked rooms from a to d

initial ='i'  # initial starting rooom
goal = 'x'  # final destination goal
route = []  # keep rooms visted as a route
queue = []

queue.append(initial) # the starting (initial) room

print("Robot at: ", initial)
print("Goal is: ", goal)
compter = 0

# the loop explores our 
while queue != None:  # if queue then stop
    headq = queue[0][0]  # pull first item in queue into head
    route.append(headq)  # add each locaton (headq) to route
    tail = queue[1:len(queue)]  # get tail of queue after first item removed
    nextlink = findworld(headq[0][0])  # returns the next linked room
    newqueue = []  # make a new queue including the next link to follow + tail 
    newqueue += nextlink
    newqueue += tail
    queue = newqueue
    print (route)

    if(headq == goal):
        break

但是,我想实现它,所以它适用于图形成员,如a1,a2,a3,z11等。

但是选择数组元素存在问题,这些元素给了我无限循环。

我做的是

def findworld(x): 
    znext = 'z0'
    c = 0

    for c in range(len(world)):
        f = world[c][0]
        t = world[c][1]
        print("f ",f)
        print("t ",t)
        print("find x", x)
        print("find world", world[c][1])             
        if(x == world[c][0]) : znext = world[c][1]; break
    return znext;

world = [['a0', 'a1'],
         ['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4'],
         ['a4', 'a5'], ['a5', 'a6'], ['a6', 'b6'],
         ['b6', 'd6'], ['d6', 'e6'], ['e6', 'f6'],
         ['f6', 'g6'], ['g6', 'g5'], ['g5', 'g4'],
         ['g4', 'g3'], ['g3', 'g2'], ['g2', 'g1'],
         ['g1', 'h1'], ['h1', 'j1'], ['j1', 'j2'],
         ['j2', 'j3'], ['j3', 'j4'], ['j4', 'j5'],
         ['j5', 'j6'], ['j7', 'j8'], ['j8', 'j9'],
         ['j9', 'k9'], ['k9', 'l9'], ['l9', 'm9'] ]

initial ='a0'
goal = 'a4'
route = []
queue = []

queue.append(initial)

print("Robot at: ", initial)
print("Goal is: ", goal)
compter = 0

while queue != None:
    headq = queue
    print("hq ", headq)
    route.append(headq)
    compter = compter + 1
    print (compter)
    tail = queue[1:len(queue)]
    print("tail ", tail)
    nextlink = findworld(headq)
    print("nextlink ", nextlink)
    newqueue = []
    newqueue += nextlink
    newqueue += tail
    queue = newqueue
    print("q", queue)

    if(headq == goal):
        break

但是因为我做了headq =队列,所以它总是同一个成员。我以前从未尝试过python。我怎么能这样做它可以作为第一个代码?

python depth-first-search
1个回答
1
投票

有几个重大问题。第一个是您将列表x与节点列表中的字符串world[c][0]进行比较。这个检查将永远是False,因此你的无限循环。另一个原因是你在一两个地方混淆了你的清单的维度。这样做的一个结果是您将节点名称的两个字符分开,并且单个字符永远不会等于整个节点标签。

尝试一次更改一个方面。使用大量的print语句来跟踪您传递和操作的值。在每个决策点,确保您正在操纵您认为自己拥有的价值观。这将一直失败,直到你结束,但你会知道你在每一点做什么。

© www.soinside.com 2019 - 2024. All rights reserved.