Advertisement
Guest User

coins

a guest
Dec 14th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. from collections import namedtuple
  2.  
  3. Coordinates = namedtuple("Coordinates", ["x", "y"])
  4.  
  5. START_CHAR = "s"
  6.  
  7. class Cell:
  8.     def __init__(self, value):
  9.         self.value = int(value) if value != START_CHAR else value
  10.         self.path = []
  11.  
  12.     def __repr__(self):
  13.         return str(self.value)
  14.  
  15.  
  16. def print_cell_field(cell_field):
  17.     for row in cell_field:
  18.         for elem in row:
  19.             print(elem, end=" ")
  20.         print()
  21.  
  22.  
  23. def find_the_way(cell_field):
  24.     print("finding da way...")
  25.     start = None
  26.     for i, row in enumerate(cell_field):
  27.         for j, cell in enumerate(row):
  28.             if cell.value == START_CHAR:
  29.                 print(f"start coords: {i}, {j}")
  30.                 start = Coordinates(i, j)
  31.                 cell_field[i][j].value = 0
  32.     if start is None:
  33.         print("no start point found!")
  34.         raise Exception("No start point")
  35.  
  36.     print_cell_field(cell_field)
  37.     for i in range(start.x, len(cell_field)):
  38.         for j in range(start.y, len(cell_field[0])):
  39.             print(f"i'm in ({i}, {j}) cell...")
  40.  
  41.             _cell = cell_field[i][j]
  42.             _cell_left = cell_field[i][j - 1]
  43.             _cell_above = cell_field[i - 1][j]
  44.  
  45.             _cell.path.append((i,j))
  46.             if i == start.x and j == start.y:
  47.                 continue
  48.  
  49.             if i == start.x:
  50.                 _cell.value = _cell.value + _cell_left.value
  51.                 _cell.path.extend(_cell_left.path)
  52.                 continue
  53.  
  54.             if j == start.y:
  55.                 _cell.value = _cell.value + _cell_above.value
  56.                 _cell.path.extend(_cell_above.path)
  57.                 continue
  58.  
  59.             if _cell_above.value >= _cell_left.value:
  60.                 _cell.value = _cell.value + _cell_above.value
  61.                 _cell.path.extend(_cell_above.path)
  62.             else:
  63.                 _cell.value = _cell.value + _cell_left.value
  64.                 _cell.path.extend(_cell_left.path)
  65.     return cell_field[len(cell_field) - 1][len(cell_field[0]) - 1]
  66.  
  67.  
  68. def main():
  69.     # size_input_string = input("enter the size of cell_field (n, m): ")
  70.     size_input_string = input()
  71.     n, m = size_input_string.split()
  72.     n = int(n)
  73.     m = int(m)
  74.     cell_field = []
  75.     for i in range(n):
  76.         row = []
  77.         # row_input_string = input(f"enter row {i}: ")
  78.         row_input_string = input()
  79.         for cell_value in row_input_string.split():
  80.             row.append(Cell(cell_value))
  81.         cell_field.append(row)
  82.     print_cell_field(cell_field)
  83.  
  84.     last_cell = find_the_way(cell_field)
  85.     path =  list(reversed(last_cell.path))
  86.     print(last_cell.value)
  87.     print(path)
  88.  
  89.  
  90. if __name__ == "__main__":
  91.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement