Advertisement
George_Ivanov05

0.2

Oct 17th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. import sys
  2. from io import StringIO
  3.  
  4. test_input1 = """Hello
  5. 4
  6. P---
  7. Mark
  8. -l-y
  9. --e-
  10. 4
  11. down
  12. right
  13. right
  14. right
  15. """
  16.  
  17. test_input2 = """Initial
  18. 5
  19. -----
  20. t-r--
  21. --Pa-
  22. --S--
  23. z--t-
  24. 4
  25. up
  26. left
  27. left
  28. left
  29. """
  30.  
  31. sys.stdin = StringIO(test_input1)
  32.  
  33.  
  34. def read_matrix():
  35.     num = int(input())
  36.     matrx = []
  37.     for _ in range(num):
  38.         matrx.append(list(input()))
  39.  
  40.     return matrx
  41.  
  42.  
  43. def is_index_valid(row, col, size):
  44.     if 0 <= row < size and 0 <= col < size:
  45.         return True
  46.     return False
  47.  
  48.  
  49. WORD = input()
  50. matrix = read_matrix()
  51. n = len(matrix)
  52.  
  53. player_row, player_col = 0, 0
  54. for r in range(n):
  55.     for c in range(n):
  56.         if matrix[r][c] == "P":
  57.             player_row = r
  58.             player_col = c
  59.             break
  60.  
  61. found_letters = []
  62.  
  63. number_command = int(input())
  64. for i in range(number_command):
  65.     command = input()
  66.     if command == "up":
  67.         if is_index_valid(player_row - 1, player_col, n):
  68.             if matrix[player_row - 1][player_col] == "-":
  69.                 matrix[player_row][player_col] = "-"
  70.                 player_row -= 1
  71.                 matrix[player_row][player_col] = "P"
  72.             elif matrix[player_row - 1][player_col].isalpha():
  73.                 matrix[player_row][player_col] = "-"
  74.                 player_row -= 1
  75.                 found_letters.append(matrix[player_row][player_col])
  76.                 matrix[player_row][player_col] = "P"
  77.         else:
  78.             if len(found_letters) > 0:
  79.                 found_letters.pop()
  80.                 break
  81.     elif command == "down":
  82.         if is_index_valid(player_row + 1, player_col, n):
  83.             if matrix[player_row + 1][player_col] == "-":
  84.                 matrix[player_row][player_col] = "-"
  85.                 player_row += 1
  86.                 matrix[player_row][player_col] = "P"
  87.             elif matrix[player_row + 1][player_col].isalpha():
  88.                 matrix[player_row][player_col] = "-"
  89.                 player_row += 1
  90.                 found_letters.append(matrix[player_row][player_col])
  91.                 matrix[player_row][player_col] = "P"
  92.         else:
  93.             if len(found_letters) > 0:
  94.                 found_letters.pop()
  95.                 break
  96.     elif command == "left":
  97.         if is_index_valid(player_row, player_col - 1, n):
  98.             if matrix[player_row][player_col - 1] == "-":
  99.                 matrix[player_row][player_col] = "-"
  100.                 player_col -= 1
  101.                 matrix[player_row][player_col] = "P"
  102.             elif matrix[player_row][player_col - 1].isalpha():
  103.                 matrix[player_row][player_col] = "-"
  104.                 player_col -= 1
  105.                 found_letters.append(matrix[player_row][player_col])
  106.                 matrix[player_row][player_col] = "P"
  107.         else:
  108.             if len(found_letters) > 0:
  109.                 found_letters.pop()
  110.                 break
  111.     elif command == "right":
  112.         if is_index_valid(player_row, player_col + 1, n):
  113.             if matrix[player_row][player_col + 1] == "-":
  114.                 matrix[player_row][player_col] = "-"
  115.                 player_col += 1
  116.                 matrix[player_row][player_col] = "P"
  117.             elif matrix[player_row][player_col + 1].isalpha():
  118.                 matrix[player_row][player_col] = "-"
  119.                 player_col += 1
  120.                 found_letters.append(matrix[player_row][player_col])
  121.                 matrix[player_row][player_col] = "P"
  122.         else:
  123.             if len(found_letters) > 0:
  124.                 found_letters.pop()
  125.                 break
  126.  
  127. string = ""
  128. for i in found_letters:
  129.     string += i
  130.  
  131. print(WORD + string)
  132. for i in matrix:
  133.     print(''.join(i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement