Advertisement
mmishanchyk

Retake Exam - 16 December 2020 (problem2)

Jun 22nd, 2021
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. text = list(input())
  2. n = int(input())
  3.  
  4. matrix = []
  5.  
  6. for i in range(n):
  7.     matrix.append([el for el in input()])
  8.  
  9. m = int(input())
  10.  
  11. for row in range(n):
  12.     for col in range(n):
  13.         if matrix[row][col] == "P":
  14.             start_row = row
  15.             start_col = col
  16. matrix[start_row][start_col] = "-"
  17.  
  18. for i in range(m):
  19.     command = input()
  20.  
  21.     if command == "up":
  22.         new_row = start_row - 1
  23.         new_col = start_col
  24.  
  25.         if new_row not in range(n) or new_col not in range(n):
  26.             text.pop()
  27.             new_row = start_row
  28.             new_col = start_col
  29.         else:
  30.             next_point = matrix[new_row][new_col]
  31.             if next_point != "-":
  32.                 text.append(next_point)
  33.                 matrix[new_row][new_col] = "-"
  34.  
  35.     elif command == "down":
  36.         new_row = start_row + 1
  37.         new_col = start_col
  38.  
  39.         if new_row not in range(n) or new_col not in range(n):
  40.             text.pop()
  41.             new_row = start_row
  42.             new_col = start_col
  43.         else:
  44.             next_point = matrix[new_row][new_col]
  45.             if next_point != "-":
  46.                 text.append(next_point)
  47.                 matrix[new_row][new_col] = "-"
  48.  
  49.     elif command == "left":
  50.         new_row = start_row
  51.         new_col = start_col - 1
  52.  
  53.         if new_row not in range(n) or new_col not in range(n):
  54.             text.pop()
  55.             new_row = start_row
  56.             new_col = start_col
  57.         else:
  58.             next_point = matrix[new_row][new_col]
  59.             if next_point != "-":
  60.                 text.append(next_point)
  61.                 matrix[new_row][new_col] = "-"
  62.  
  63.     elif command == "right":
  64.         new_row = start_row
  65.         new_col = start_col + 1
  66.  
  67.         if new_row not in range(n) or new_col not in range(n):
  68.             text.pop()
  69.             new_row = start_row
  70.             new_col = start_col
  71.         else:
  72.             next_point = matrix[new_row][new_col]
  73.             if next_point != "-":
  74.                 text.append(next_point)
  75.                 matrix[new_row][new_col] = "-"
  76.  
  77.     start_row = new_row
  78.     start_col = new_col
  79.  
  80.  
  81. matrix[start_row][start_col] = "P"
  82. print("".join(text))
  83. for row in matrix:
  84.     print("".join(row))
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement