unkSonert

Untitled

Nov 21st, 2020
727
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from copy import deepcopy
  2.  
  3. matrix = [[0, 1, 0, 0, 0, 0],
  4.           [0, 0, 0, 1, 0, 1],
  5.           [0, 1, 0, 1, 0, 0],
  6.           [0, 1, 1, 1, 1, 0],
  7.           [0, 0, 0, 1, 0, 0],
  8.           [0, 1, 0, 1, 0, 2]]
  9.  
  10. resultMatrix = deepcopy(matrix)
  11.  
  12. def _findPath(x, y, i):
  13.     if x < 0 or y < 0 or x > 5 or y > 5 or matrix[y][x] == 1:
  14.         return []
  15.  
  16.     if matrix[y][x] == 2:
  17.         return [[x, y]]
  18.  
  19.     if matrix[y][x] != 0:
  20.         return []
  21.  
  22.     matrix[y][x] = "passed"
  23.  
  24.     upResult = _findPath(x, y + 1, i + 1)
  25.     downResult = _findPath(x, y - 1, i + 1)
  26.     leftResult = _findPath(x - 1, y, i + 1)
  27.     rightResult = _findPath(x + 1, y, i + 1)
  28.  
  29.     if len(upResult) != 0:
  30.         upResult.append([x, y])
  31.         return upResult
  32.  
  33.     if len(downResult) != 0:
  34.         downResult.append([x, y])
  35.         return downResult
  36.  
  37.     if len(leftResult) != 0:
  38.         leftResult.append([x, y])
  39.         return leftResult
  40.  
  41.     if len(rightResult) != 0:
  42.         rightResult.append([x, y])
  43.         return rightResult
  44.  
  45.     return []
  46.  
  47. def findPathNumeric(x, y):
  48.     path = _findPath(x, y, 1)
  49.     path.reverse()
  50.  
  51.     i = 1
  52.     for iter in path:
  53.         resultMatrix[iter[1]][iter[0]] = i
  54.         i += 1
  55.  
  56.     for line in resultMatrix:
  57.         print("{0[0]} {0[1]} {0[2]} {0[3]} {0[4]} {0[5]}".format(line))
  58.  
  59. def findPathGraphic(x, y):
  60.     path = _findPath(x, y, 1)
  61.  
  62.     prev = []
  63.  
  64.     for iter in path:
  65.         if len(prev) == 0:
  66.             resultMatrix[iter[1]][iter[0]] = "X"
  67.             prev = iter
  68.             continue
  69.  
  70.         if prev[0] != iter[0]:
  71.             resultMatrix[iter[1]][iter[0]] = "-"
  72.  
  73.         if prev[1] != iter[1]:
  74.             resultMatrix[iter[1]][iter[0]] = "|"
  75.  
  76.         prev = iter
  77.  
  78.     for line in resultMatrix:
  79.         print("{0[0]} {0[1]} {0[2]} {0[3]} {0[4]} {0[5]}".format(line))
  80.  
  81. findPathGraphic(0, 5)
  82. #findPathNumeric(0, 5)
RAW Paste Data