Maze Barrier Placement
Imagine you're building a simple 2D maze game. A crucial part of maze generation is defining the boundaries and internal walls. This challenge focuses on programmatically placing these "barriers" within a grid, represented as a 2D list. Your task is to create a function that takes a grid and a list of barrier coordinates, and then modifies the grid to represent these barriers.
Problem Description
You need to implement a Python function place_barriers(grid, barriers) that takes two arguments:
grid: A 2D list (list of lists) representing the maze. Each inner list represents a row, and each element within an inner list represents a cell. Initially, all cells might be represented by a specific character (e.g., a space ' ').barriers: A list of tuples, where each tuple(row, col)specifies the coordinates of a barrier to be placed.
The function should modify the grid in-place, changing the character at the specified barrier coordinates to a barrier symbol (e.g., '#').
Key Requirements:
- The function must accept a 2D list and a list of coordinate tuples.
- It must update the
gridby placing the barrier symbol at the given coordinates. - The barrier symbol to be used is '#'.
- The original
gridshould be modified directly (in-place).
Expected Behavior:
- If a barrier coordinate is within the bounds of the grid, the corresponding cell in the
gridshould be updated to '#'. - If a barrier coordinate is outside the bounds of the grid, it should be ignored, and the grid should remain unchanged for that coordinate.
Edge Cases to Consider:
- An empty
barrierslist. - A
barrierslist containing coordinates that are out of bounds for the givengrid. - A
gridthat is empty or has empty rows. - Placing barriers on cells that already contain other elements (though for this challenge, assume initial cells are uniform, e.g., spaces).
Examples
Example 1:
Input:
grid = [
[' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ']
]
barriers = [(0, 1), (1, 2), (2, 0)]
Output:
[
[' ', '#', ' ', ' '],
[' ', ' ', '#', ' '],
['#', ' ', ' ', ' ']
]
Explanation: The barriers at (0, 1), (1, 2), and (2, 0) have been placed in the grid.
Example 2:
Input:
grid = [
[' ', ' ', ' '],
[' ', ' ', ' ']
]
barriers = [(0, 0), (1, 1), (5, 5), (-1, 0)]
Output:
[
['#', ' ', ' '],
[' ', '#', ' ']
]
Explanation: Barriers at (0, 0) and (1, 1) are placed. The coordinates (5, 5) and (-1, 0) are out of bounds and are ignored.
Example 3:
Input:
grid = [
[' ', ' ', ' '],
[' ', ' ', ' ']
]
barriers = []
Output:
[
[' ', ' ', ' '],
[' ', ' ', ' ']
]
Explanation: An empty barriers list results in no changes to the grid.
Constraints
- The
gridwill be a list of lists. - The inner lists (rows) of the
gridwill have consistent lengths. - The
barrierswill be a list of tuples. - Each tuple in
barrierswill contain two integers representing(row, col). - The number of rows in the
gridwill be between 0 and 100. - The number of columns in the
gridwill be between 0 and 100. - The number of barriers in the
barrierslist will be between 0 and 500.
Notes
- Remember that Python uses 0-based indexing for lists.
- When checking for out-of-bounds coordinates, consider both negative indices and indices exceeding the grid dimensions.
- The function should return
Noneas it modifies the grid in-place.