Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2021
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def is_in_range(row, col, n):
  5.     if 0 <= row < n and 0 <= col < n:
  6.         return True
  7.     return False
  8.  
  9. size = int(input())
  10. commands = deque([x for x in input().split()])
  11.  
  12. field = []
  13.  
  14. directions = {
  15.     "up": (-1, 0),
  16.     "down": (1, 0),
  17.     "right": (0, 1),
  18.     "left": (0, -1)
  19. }
  20.  
  21. expected_coals = 0
  22. coals = 0
  23. currRow = -1  #следи позиция на s
  24. currCol = -1  #следи позиция на s
  25.  
  26. for row in range(size):
  27.     field.append(input().split())
  28.  
  29. for row2 in range(len(field)):
  30.     for col2 in range(len(field)):
  31.         if field[row2][col2] == "c":
  32.             expected_coals += 1
  33.  
  34. for row in range(len(field)):   #намира позиция на s
  35.     for col in range(len(field)):
  36.         if field[row][col] == "s":
  37.             currRow = row
  38.             currCol = col
  39.  
  40. while commands:
  41.     if commands:
  42.         current_command = commands.popleft()
  43.         new_row = currRow + directions[current_command][0]
  44.         new_col = currCol + directions[current_command][1]
  45.         if is_in_range(new_row, new_col, size):
  46.             if field[new_row][new_col] == "*":
  47.                 field[currRow][currCol], field[new_row][new_col] = "*", "s"
  48.             elif field[new_row][new_col] == "e":
  49.                 print(f"Game over! ({new_row}, {new_col})")
  50.                 quit()   #трябва да спре програмата
  51.             elif field[new_row][new_col] == "c":
  52.                 coals += 1
  53.                 field[currRow][currCol], field[new_row][new_col] = "*", "s"
  54.             currRow = new_row
  55.             currCol = new_col
  56. if coals == expected_coals:
  57.     print(f"You collected all coal! ({currRow}, {currCol})")
  58. else:
  59.     print(f"{expected_coals - coals} pieces of coal left. ({currRow}, {currCol})")
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement