Advertisement
horozov86

Easter_Bunny_with_lambda

Jun 6th, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. directions = {
  2.     "up": lambda row, col: (row - 1, col),
  3.     "down": lambda row, col: (row + 1, col),
  4.     "left": lambda row, col: (row, col - 1),
  5.     "right": lambda row, col: (row, col + 1),
  6. }
  7.  
  8. size = int(input())
  9. matrix = []
  10. bunny_row = 0
  11. bunny_col = 0
  12. for row in range(size):
  13.     inner_list = input().split()
  14.     matrix.append(inner_list)
  15.  
  16.     for col in range(size):
  17.         if inner_list[col] == "B":
  18.             bunny_row, bunny_col = row, col
  19.  
  20. best_dir = ""
  21. best_path = []
  22. best_sum = float("-inf")
  23. for direction, moving in directions.items():
  24.     current_row, current_col = bunny_row, bunny_col
  25.  
  26.     path = []
  27.     current_sum = 0
  28.     while True:
  29.         current_row, current_col = moving(current_row, current_col)
  30.  
  31.         if current_row < 0 or current_col < 0 or current_row >= size or current_col >= size:
  32.             break
  33.         if matrix[current_row][current_col] == "X":
  34.             break
  35.  
  36.         path.append([current_row, current_col])
  37.         current_sum += int(matrix[current_row][current_col])
  38.  
  39.     if current_sum > best_sum and path:
  40.         best_sum = current_sum
  41.         best_dir = direction
  42.         best_path = path
  43.  
  44. print(best_dir)
  45. for path in best_path:
  46.     print(path)
  47.  
  48. print(best_sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement