Advertisement
GeorgiLukanov87

09. Miner - Multidimensional Lists - Exercise 1

Aug 28th, 2022 (edited)
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. #   09. Miner
  2. #   https://judge.softuni.org/Contests/Practice/Index/1835#8
  3. #   Multidimensional Lists - Exercise 1
  4. ============================================================================================================
  5. # NEW VERSION !
  6.  
  7. def is_inside(r, c):
  8.     return 0 <= r < size and 0 <= c < size
  9.  
  10.  
  11. def get_next_position(direction, r, c):
  12.     if direction == 'left':
  13.         return r, c - 1
  14.     elif direction == 'right':
  15.         return r, c + 1
  16.     elif direction == 'up':
  17.         return r - 1, c
  18.     elif direction == 'down':
  19.         return r + 1, c
  20.  
  21.  
  22. size = int(input())
  23. matrix = []
  24. coal = 0
  25. row, col = 0, 0
  26. steps = input().split()
  27.  
  28. for row_index in range(size):
  29.     matrix.append(input().split())
  30.     for col_index in range(size):
  31.         if matrix[row_index][col_index] == 'c':
  32.             coal += 1
  33.         if matrix[row_index][col_index] == 's':
  34.             row, col = row_index, col_index
  35.  
  36. game_over = False
  37. for step in steps:
  38.     next_row, next_col = get_next_position(step, row, col)
  39.     if not is_inside(next_row, next_col):
  40.         continue
  41.     else:
  42.         row, col = next_row, next_col
  43.         current_pos = matrix[row][col]
  44.  
  45.         if current_pos == 'c':
  46.             coal -= 1
  47.             matrix[row][col] = '*'
  48.             if coal == 0:
  49.                 print(f"You collected all coal! ({row}, {col})")
  50.                 break
  51.  
  52.         elif current_pos == 'e':
  53.             print(f"Game over! ({row}, {col})")
  54.             game_over = True
  55.             break
  56.  
  57. if coal > 0 and not game_over:
  58.     print(f"{coal} pieces of coal left. ({row}, {col})")
  59.  
  60. ===================================================================================
  61.  
  62. #   09. Miner
  63. # OLD VERSION
  64.  
  65.  
  66. def find_all_coals_the_field_func(some_matrix):
  67.     total_coal = 0
  68.     for row_index in range(len(some_matrix)):
  69.         for col_index in range(len(some_matrix)):
  70.             if matrix[row_index][col_index] == 'c':
  71.                 total_coal += 1
  72.     return total_coal
  73.  
  74.  
  75. def find_start_point_func(some_matrix):
  76.     # Finding the start point - row and col with letter 's'
  77.     for row_index in range(len(some_matrix)):
  78.         for col_index in range(len(some_matrix)):
  79.             if matrix[row_index][col_index] == 's':
  80.                 return row_index, col_index
  81.  
  82.  
  83. def is_inside_func(current_row, current_col, default_size):  # SQUARE matrix => rows = cols
  84.     # Is this point inside in the sub_matrix
  85.     return 0 <= current_row < default_size and 0 <= current_col < default_size
  86.  
  87.  
  88. size = int(input())
  89. matrix = []
  90. commands = input().split()
  91.  
  92. for _ in range(size):
  93.     current_line = input().split()
  94.     matrix.append(current_line)
  95.  
  96. row, col = find_start_point_func(matrix)
  97. coals_cap = find_all_coals_the_field_func(matrix)
  98. ######################
  99. #       0 1 2 3 4     #
  100. #                     #
  101. #   0   * * * c *     #
  102. #   1   * * * e *     #
  103. #   2   * * c * *     #
  104. #   3   S * * c *     #
  105. #   4   * * c * *     #
  106. #                     #
  107. ######################
  108. coals_found = 0
  109. start = matrix[row][col]
  110.  
  111. finished = False
  112. for current_cmd in commands:
  113.     if current_cmd == 'up' and is_inside_func(row - 1, col, size):
  114.         next_move = matrix[row - 1][col]
  115.         row -= 1
  116.        
  117.     elif current_cmd == 'down' and is_inside_func(row + 1, col, size):
  118.         next_move = matrix[row + 1][col]
  119.         row += 1
  120.        
  121.     elif current_cmd == 'right' and is_inside_func(row, col + 1, size):
  122.         next_move = matrix[row][col + 1]
  123.         col += 1
  124.        
  125.     elif current_cmd == 'left' and is_inside_func(row, col - 1, size):
  126.         next_move = matrix[row][col - 1]
  127.         col -= 1
  128.     else:
  129.         # if current_cell is INVALID! => continue to the next iteration
  130.         continue
  131.  
  132.     if next_move == '*':
  133.         pass
  134.     elif next_move == 'e':
  135.         print(f'Game over! {row, col}')
  136.         finished = True
  137.         break
  138.     elif next_move == 'c':
  139.         coals_found += 1
  140.         matrix[row][col] = '*'
  141.  
  142. if not finished:
  143.     if coals_found == coals_cap:
  144.         print(f'You collected all coal! {(row, col)}')
  145.     else:
  146.         print(f'{coals_cap - coals_found} pieces of coal left. {(row, col)}')
  147.        
  148.        
  149.        
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement