python - Shortest path through maze ignoring one wall -


so taking on foo.bar challenge , need prepare bunnies escape. whenever run test case on code works, still says failed on submit.

the challenge shortest path through matrix maze m x n 2 < m,n < 20 , 1 wall may removed.

my solution find possible paths finish start, counting how many walls crossed along way. if number of walls 1 or fewer, solution valid , length recorded.

path_dict = {} min_solution = none  def answer(maze):     path = none     traverse (maze, path, 0, 0)     global min_solution     return min_solution  def traverse (maze, path, x, y):     global path_dict     global min_solution       cell_str = '(%d,%d)' % (x, y)      x_max = len (maze) - 1     y_max = len (maze[0]) - 1      # check x , y valid     if x < 0 or x > x_max:         return      if y < 0 or y > y_max:         return       # check not visited cell     if path , cell_str in path:         return       total = none     if path:         total = path_dict [path]      f_cell = '(%d,%d)' % (x_max, y_max)      if path , f_cell in path:         return      # check if final cell     if x == x_max , y == y_max:         if not path [-5:] == f_cell:             path += f_cell             path_dict [path] = total         solution_len = len (path.split (')('))         if not min_solution:             min_solution = solution_len         elif solution_len < min_solution:             min_solution = solution_len         #maze [x][y] = 2          return       #if path , path [-5:] == f_cell:     #   return      # not final cell - value, add count      # add path string mem     if path:         path += cell_str         cell_val = maze [x][y]         total += cell_val         # many walls hit, stop checking path         if total >= 2:             return      else:         path = cell_str         total = 0      path_dict [path] = total      if min_solution , len (path.split (')(')) > min_solution:         return      # move down, right, up, left     traverse (maze, path, x + 1, y)     traverse (maze, path, x, y + 1)     traverse (maze, path, x - 1, y)     traverse (maze, path, x, y - 1)      return 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -