Advertisement
Onesible

Untitled

Feb 2nd, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. def valid_position(current_row, current_column):
  2.     if 0 <= current_row < rows and 0 <= current_column < cols:
  3.         return True
  4.     else:
  5.         return False
  6.  
  7. rows, cols = [int(x) for x in input().split(",")]
  8. matrix = []
  9.  
  10. mouse_position = []
  11. total_cheese = 0
  12.  
  13. directions = {
  14.     "up": (-1, 0),
  15.     "down": (1, 0),
  16.     "left": (0, -1),
  17.     "right": (0, 1)
  18. }
  19. for row in range(rows):
  20.     matrix.append(list(input()))
  21.  
  22.     if "M" in matrix[row]:
  23.         mouse_position = [row, matrix[row].index("M")]
  24.         matrix[row][matrix[row].index("M")] = "*"
  25.  
  26.     if "C" in matrix[row]:
  27.         total_cheese += matrix[row].count("C")
  28.  
  29. while total_cheese:
  30.     command = input()
  31.  
  32.     if command == "danger" and total_cheese:
  33.         matrix[mouse_position[0]][mouse_position[1]] = "M"
  34.         print("Mouse will come back later!")
  35.         break
  36.  
  37.     row = mouse_position[0] + directions[command][0]
  38.     col = mouse_position[1] + directions[command][1]
  39.  
  40.     if valid_position(row, col):
  41.         if matrix[row][col] == "@":
  42.             continue
  43.         if matrix[row][col] == "*":
  44.              mouse_position = [row, col]
  45.         elif matrix[row][col] == "C":
  46.             total_cheese -= 1
  47.             mouse_position = [row, col]
  48.  
  49.             if total_cheese == 0:
  50.                 matrix[row][col] = "M"
  51.                 print("Happy mouse! All the cheese is eaten, good night!")
  52.                 break
  53.             else:
  54.                 matrix[row][col] = "*"
  55.         elif matrix[row][col] == "T":
  56.             matrix[row][col] = "M"
  57.             print("Mouse is trapped!")
  58.             break
  59.  
  60.         mouse_position = [row, col]
  61.     else:
  62.         matrix[mouse_position[0]][mouse_position[1]] = "M"
  63.         print("No more cheese for tonight!")
  64.         break
  65. [print(*row, sep="") for row in matrix]
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement