Advertisement
horozov86

The Squirrel

Jun 6th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. def moving(row, col, direction):
  2.     if direction == "up":
  3.         return row - 1, col
  4.  
  5.     if direction == "down":
  6.         return row + 1, col
  7.  
  8.     if direction == "left":
  9.         return row, col - 1
  10.  
  11.     if direction == "right":
  12.         return row, col + 1
  13.  
  14.  
  15. size = int(input())
  16. directions = input().split(', ')
  17. matrix = []
  18. s_row = 0
  19. s_col = 0
  20.  
  21. for row in range(size):
  22.     row_elements = list(input())
  23.  
  24.     for col in range(size):
  25.         if row_elements[col] == "s":
  26.             s_row = row
  27.             s_col = col
  28.     matrix.append(row_elements)
  29.  
  30. hazelnuts = 0
  31. while True:
  32.     matrix[s_row][s_col] = "*"
  33.     for direction in directions:
  34.         next_row, next_col = moving(s_row, s_col, direction)
  35.  
  36.         if next_row < 0 or next_col < 0 or next_row >= size or next_col >= size:
  37.             print(f'The squirrel is out of the field.')
  38.             break
  39.         s_row, s_col = next_row, next_col
  40.  
  41.         if matrix[s_row][s_col] == "t":
  42.             print(f'Unfortunately, the squirrel stepped on a trap...')
  43.             break
  44.  
  45.         if matrix[s_row][s_col] == "h":
  46.             hazelnuts += 1
  47.            
  48.         if hazelnuts == 3:
  49.             print(f'Good job! You have collected all hazelnuts!')
  50.             break
  51.  
  52.     print(f'Hazelnuts collected: {hazelnuts}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement