Depth Search in Python

I am trying to do a Depth-First search in Python, but it does not work.

Basically, we have a solitaire board:

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

1 is a snap, and 0 is an open spot. You must move the binding one at a time DOUBLE SLOTS back or forward ONLY to an empty space. If you jump over another binding, the process becomes an empty slot. You do this until one binding remains. So basically the game is as follows:

[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 is 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 are:

  • Is it right to do a depth search? First for this?
  • My algorithm does not work !!! He is stuck. I struggled with this for several days before asking here, so please help.
  • Looks like I'm not following DRY, any suggestions?
  • omg help me?
+3
4

DFS , "" "" , , ()

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()

, , , ( ), .

( ) . ? possiblesuccessors isending ( , , , ; -).

+8

, , . DFS - ( ). ?

+1

, , . , . , , , , , , .

"" . , .

- (pythonic psuedo codeish english):

def try_next_move(self, board):
    for each of the pegs in the board:
        if the peg can be moved:
            new_board = board with the peg moved
            if new_board is solved:
                return True
            if self.try_next_move(new_board):
                return True
            # That move didn't lead to a solution. Try the next.
    # No move  worked.
    return False
0

, succ . , succ , . .

- , create_new_node, , node, . , node , , , .

Also, when checking for the possibility of moving back in succ, you are trying to do this if pos > 2. This is too restrictive, pos > 1will also be fine.

0
source

Source: https://habr.com/ru/post/1730018/


All Articles