python - How to try all possible paths? -


i need try possible paths, branching every time hit point. there <128 possible paths problem, no need worry exponential scaling.

  1. i have player can take steps through field. player takes step, , on step there encounter.
  2. there 2 options when encounter found: i) input 'b' or ii) input 'g'.
  3. i try both , continue repeating until end of field reached. end goal have tried possibilities.

here template, in python, talking (step object returns next step using next()):

from row_maker_inlined import step  def main():     initial_stats = {'n':1,'step':250,'o':13,'i':113,'dng':0,'inp':'empty'}     player = step(initial_stats)     end_of_field = 128      # walk until reaching encounter:     while player.step['n'] < end_of_field:         player.next()         if player.step['enc']:             print 'an encounter has been reached.' #           perform input on encounter step:             player.input = 'b'  #           make branch of player? #           perform on branch: #           player.input = 'g' #   keep doing this, , branching on each encounter, until end reached. 

as can see, problem rather simple. have no idea, beginner programmer, how solve such problem.

i believe may need use recursion in order keep branching. not understand how 1 'makes branch' using recursion, or else.

what kind of solution should looking at?

you should looking @ search algorithms breath first search (bfs) , depth first search (dfs).

wikipedia has pseudo-code implementation of bfs:

  procedure bfs(g, v)     let q queue     q.enqueue(v)     label v discovered     while q not empty         v← q.dequeue()         edges v w in g.adjacentedges(v)             if w not labeled discovered                 q.enqueue(w)                 label w discovered 

essentially, when reach "encounter" want add point queue @ end. pick first element off of queue , explore it, putting children queue, , on. it's non-recursive solution simple enough want.

dfs similar instead of picking first element form queue, pick last. makes explore single path way dead end before coming explore another.

good luck!


Comments