Advertisement
horozov86

Easter_Bunny

Jun 6th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. def move_up(row, col):
  2.     return row - 1, col
  3.  
  4.  
  5. def move_down(row, col):
  6.     return row + 1, col
  7.  
  8.  
  9. def move_left(row, col):
  10.     return row, col - 1
  11.  
  12.  
  13. def move_right(row, col):
  14.     return row, col + 1
  15.  
  16. size = int(input())
  17. matrix = []
  18. bunny_row = 0
  19. bunny_col = 0
  20.  
  21. for row in range(size):
  22.     row_elements = input().split()
  23.     matrix.append(row_elements)
  24.     for col in range(size):
  25.         if row_elements[col] == "B":
  26.             bunny_row = row
  27.             bunny_col = col
  28.  
  29. directions = {
  30.     "up": move_up,
  31.     "down": move_down,
  32.     "left": move_left,
  33.     "right": move_right
  34.  
  35. }
  36.  
  37. best_sum = float("-inf")
  38. best_direction = ""
  39. best_path = []
  40. for direction, step in directions.items():
  41.     current_row, current_col = bunny_row, bunny_col
  42.  
  43.     path = []
  44.     current_sum = 0
  45.     while True:
  46.         current_row, current_col = step(current_row, current_col) # извиквам функцията
  47.  
  48.         if current_row < 0 or current_col < 0 or current_row >= size or current_col >= size or matrix[current_row][current_col] == "X":
  49.             break
  50.         path.append([current_row, current_col])
  51.         current_sum += int(matrix[current_row][current_col])
  52.  
  53.     if current_sum > best_sum and path:
  54.         best_sum = current_sum
  55.         best_direction = direction
  56.         best_path = path
  57.  
  58. print(best_direction)
  59. for path in best_path:
  60.     print(path)
  61.  
  62. print(best_sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement