Advertisement
Guest User

Easter Bunny Multidimensional Lists

a guest
Feb 15th, 2022
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. n = int(input())
  2. matrix = []
  3. bunny_i = 0
  4. bunny_j = 0
  5.  
  6. # 1) traverse the matrix and find the Bunny's location; keep the Bunny's coordinates:
  7. for i in range(n):
  8.     line = input().split()
  9.     matrix.append(line)
  10.     # check the location of the bunny
  11.     for j in range(n):
  12.         if line[j] == "B":
  13.             bunny_i = i
  14.             bunny_j = j
  15.  
  16. # 2) explore every possible route - we have four possible routes - up, down, left, right:
  17. possible_route = ['up', 'down', 'left', 'right']
  18. best_direction = ''
  19. best_path = []
  20. best_score = float("-inf")
  21.  
  22. for x in possible_route:
  23.     current_row, current_col = bunny_i, bunny_j
  24.     bunny_path = []
  25.     current_score = 0
  26.  
  27.     # try to move up:
  28.     if x == 'up':
  29.         while True:
  30.             current_row, current_col = current_row-1, current_col
  31.             if current_row < 0 or current_col < 0 or current_row >= n or current_col >= n or matrix[current_row][current_col] == "X":
  32.                 break
  33.             else:
  34.                 bunny_path.append([current_row, current_col])
  35.                 current_score += int(matrix[current_row][current_col])
  36.             if current_score > best_score and bunny_path:
  37.                 best_score = current_score
  38.                 best_direction = 'up'
  39.                 best_path = bunny_path
  40.  
  41.     # try to move down:
  42.     if x == 'down':
  43.         while True:
  44.             current_row, current_col = current_row+1, current_col
  45.             if current_row < 0 or current_col < 0 or current_row >= n or current_col >= n or matrix[current_row][current_col] == "X":
  46.                 break
  47.             else:
  48.                 bunny_path.append([current_row, current_col])
  49.                 current_score += int(matrix[current_row][current_col])
  50.             if current_score > best_score and bunny_path:
  51.                 best_score = current_score
  52.                 best_direction = 'down'
  53.                 best_path = bunny_path
  54.  
  55.     # try to move left:
  56.     if x == 'left':
  57.         while True:
  58.             current_row, current_col = current_row, current_col-1
  59.             if current_row < 0 or current_col < 0 or current_row >= n or current_col >= n or matrix[current_row][current_col] == "X":
  60.                 break
  61.             else:
  62.                 bunny_path.append([current_row, current_col])
  63.                 current_score += int(matrix[current_row][current_col])
  64.             if current_score > best_score and bunny_path:
  65.                 best_score = current_score
  66.                 best_direction = 'left'
  67.                 best_path = bunny_path
  68.  
  69.     # try to move right:
  70.     if x == 'right':
  71.         while True:
  72.             current_row, current_col = current_row, current_col+1
  73.             if current_row < 0 or current_col < 0 or current_row >= n or current_col >= n or matrix[current_row][current_col] == "X":
  74.                 break
  75.             else:
  76.                 bunny_path.append([current_row, current_col])
  77.                 current_score += int(matrix[current_row][current_col])
  78.             if current_score > best_score and bunny_path:
  79.                 best_score = current_score
  80.                 best_direction = 'right'
  81.                 best_path = bunny_path
  82.  
  83. print(best_direction)
  84. for x in best_path:
  85.     print(x)
  86. print(best_score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement