Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 09. Miner
- # https://judge.softuni.org/Contests/Practice/Index/1835#8
- # Multidimensional Lists - Exercise 1
- ============================================================================================================
- # NEW VERSION !
- def is_inside(r, c):
- return 0 <= r < size and 0 <= c < size
- def get_next_position(direction, r, c):
- if direction == 'left':
- return r, c - 1
- elif direction == 'right':
- return r, c + 1
- elif direction == 'up':
- return r - 1, c
- elif direction == 'down':
- return r + 1, c
- size = int(input())
- matrix = []
- coal = 0
- row, col = 0, 0
- steps = input().split()
- for row_index in range(size):
- matrix.append(input().split())
- for col_index in range(size):
- if matrix[row_index][col_index] == 'c':
- coal += 1
- if matrix[row_index][col_index] == 's':
- row, col = row_index, col_index
- game_over = False
- for step in steps:
- next_row, next_col = get_next_position(step, row, col)
- if not is_inside(next_row, next_col):
- continue
- else:
- row, col = next_row, next_col
- current_pos = matrix[row][col]
- if current_pos == 'c':
- coal -= 1
- matrix[row][col] = '*'
- if coal == 0:
- print(f"You collected all coal! ({row}, {col})")
- break
- elif current_pos == 'e':
- print(f"Game over! ({row}, {col})")
- game_over = True
- break
- if coal > 0 and not game_over:
- print(f"{coal} pieces of coal left. ({row}, {col})")
- ===================================================================================
- # 09. Miner
- # OLD VERSION
- def find_all_coals_the_field_func(some_matrix):
- total_coal = 0
- for row_index in range(len(some_matrix)):
- for col_index in range(len(some_matrix)):
- if matrix[row_index][col_index] == 'c':
- total_coal += 1
- return total_coal
- def find_start_point_func(some_matrix):
- # Finding the start point - row and col with letter 's'
- for row_index in range(len(some_matrix)):
- for col_index in range(len(some_matrix)):
- if matrix[row_index][col_index] == 's':
- return row_index, col_index
- def is_inside_func(current_row, current_col, default_size): # SQUARE matrix => rows = cols
- # Is this point inside in the sub_matrix
- return 0 <= current_row < default_size and 0 <= current_col < default_size
- size = int(input())
- matrix = []
- commands = input().split()
- for _ in range(size):
- current_line = input().split()
- matrix.append(current_line)
- row, col = find_start_point_func(matrix)
- coals_cap = find_all_coals_the_field_func(matrix)
- ######################
- # 0 1 2 3 4 #
- # #
- # 0 * * * c * #
- # 1 * * * e * #
- # 2 * * c * * #
- # 3 S * * c * #
- # 4 * * c * * #
- # #
- ######################
- coals_found = 0
- start = matrix[row][col]
- finished = False
- for current_cmd in commands:
- if current_cmd == 'up' and is_inside_func(row - 1, col, size):
- next_move = matrix[row - 1][col]
- row -= 1
- elif current_cmd == 'down' and is_inside_func(row + 1, col, size):
- next_move = matrix[row + 1][col]
- row += 1
- elif current_cmd == 'right' and is_inside_func(row, col + 1, size):
- next_move = matrix[row][col + 1]
- col += 1
- elif current_cmd == 'left' and is_inside_func(row, col - 1, size):
- next_move = matrix[row][col - 1]
- col -= 1
- else:
- # if current_cell is INVALID! => continue to the next iteration
- continue
- if next_move == '*':
- pass
- elif next_move == 'e':
- print(f'Game over! {row, col}')
- finished = True
- break
- elif next_move == 'c':
- coals_found += 1
- matrix[row][col] = '*'
- if not finished:
- if coals_found == coals_cap:
- print(f'You collected all coal! {(row, col)}')
- else:
- print(f'{coals_cap - coals_found} pieces of coal left. {(row, col)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement