Depth-First search in Python

后端 未结 4 1514
南笙
南笙 2021-02-06 13:37

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]

4条回答
  •  我在风中等你
    2021-02-06 14:09

    The basic algorithmic problem is that the succ function always only produces just one possible move for a given board state. Even if there would be more than one possible moves, the succ function just returns the first one it can find. A depth first search needs to process all possible moves in each state.

    Further problems might then come from the fact that create_new_node, despite it's name, doesn't really create a new node, but modifies the existing one. For depth first search where you want to keep the previous node around it would be better if this function actually created a copy of the list it get's as a parameter.

    Also, when checking for the possibility to go backwards in succ, you only try to do so if pos > 2. That's too restrictive, pos > 1 would also be ok.

提交回复
热议问题