fork download
  1. def esValido(n, m, i, j, lab):
  2. return 0 <= i < m and 0 <= j < n and lab[i][j] == 0
  3.  
  4. def recorroLab(lab):
  5. m, n = len(lab), len(lab[0])
  6. movs = []
  7. recorrido(lab, m, n, 0, 0, movs, "")
  8. return min(movs)
  9.  
  10. def recorrido(lab, m, n, i, j, movs, actual):
  11. if not esValido(n, m, i, j, lab):
  12. return
  13. if i == m - 1 and j == n - 1:
  14. movs.append(actual)
  15. return
  16. lab[i][j] = 1
  17. recorrido(lab, m, n, i + 1, j, movs, actual + "D")
  18. recorrido(lab, m, n, i - 1, j, movs, actual + "U")
  19. recorrido(lab, m, n, i, j - 1, movs, actual + "L")
  20. recorrido(lab, m, n, i, j + 1, movs, actual + "R")
  21. lab[i][j] = 0
  22.  
  23. laberinto = [
  24. [0, 0, 1, 0, 0],
  25. [1, 0, 1, 0, 1],
  26. [1, 0, 0, 0, 1],
  27. [1, 0, 0, 0, 0],
  28. [0, 1, 0, 1, 0]
  29. ]
  30.  
  31. print(recorroLab(laberinto))
  32.  
  33.  
Success #stdin #stdout 0.04s 9644KB
stdin
Standard input is empty
stdout
RDDDRRRD