python实现深度优先搜索_Python中的深度优先搜索

I'm trying to do a Depth-First search in Python but it's not working.

Basically we have a peg-solitaire board:

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

1's represent a peg, and 0 is an open spot. You must move a peg one at a time TWO SLOTS backwards or forward ONLY to an empty spot. If you jump over another peg in the process it becomes an empty slot. You do this until one peg remains. So basically, a game goes like:

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

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

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

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

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

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

[1, 0, 0, 0, 0, 0, 0, 1, 0, 1] #etc until only 1 peg left

Here's what I have:

class MiniPeg():

def start(self):

''' returns the starting board '''

board = [1,1,1,1,1,0,1,1,1,1]

return board

def goal(self, node):

pegs = 0

for pos in node:

if pos == 1:

pegs += 1

return (pegs == 1) # returns True if there is only 1 peg

def succ(self, node):

pos = 0

for peg in node:

if peg == 1:

if pos < (len(node) - 2): # try to go forward

if node[pos+2] == 0 and node[pos+1] == 1:

return create_new_node(node, pos, pos+2)

if pos > 2: # try to go backwards

if node[pos-2] == 0 and node[pos-1] == 1:

return create_new_node(node, pos, pos-2)

pos += 1

def create_new_node(node, fr, to):

node[fr] = 0

node[to] = 1

if fr > to:

node[fr-1] = 0

else:

node[fr+1] = 0

return node

if __name__ == "__main__":

s = MiniPeg()

b = s.start()

while not s.goal(b):

print b

b = s.succ(b)

So, now my questions:

Is this the right way to do a Depth-First search for this?

My algorithm doesn't work!!! It gets stuck. I've been struggling on this for days before asking here so please help.

Looks like I'm not following DRY, any suggestions?

omg help me?

解决方案

The normal way to implement DFS in a situation where each step is a "move" from a "board position" to some possible next one, until a goal is reached, is as follows (pseudocode)

seenpositions = set()

currentpositions = set([startingposition])

while currentpositions:

nextpositions = set()

for p in currentpositions:

seenpositions.add(p)

succ = possiblesuccessors(p)

for np in succ:

if np in seenpositions: continue

if isending(np): raise FoundSolution(np)

nextpositions.add(np)

currentpositions = nextpositions

raise NoSolutionExists()

You probably also want to keep backward links to be able to emit, at the end, the series of moves leading to the found solution (if any), but that's an ancillary problem.

I don't recognize a trace of this general structure (or reasonable variant thereof) in your code. Why not try to record it this way? You only need to code possiblesuccessors and isending (if you insist on keeping a position as a list you'll have to turn it into a tuple to check membership in set and add to set, but, that's pretty minor;-).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值