Hone logo
Hone
Problems

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:

  1. 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 ' ').
  2. 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 grid by placing the barrier symbol at the given coordinates.
  • The barrier symbol to be used is '#'.
  • The original grid should be modified directly (in-place).

Expected Behavior:

  • If a barrier coordinate is within the bounds of the grid, the corresponding cell in the grid should 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 barriers list.
  • A barriers list containing coordinates that are out of bounds for the given grid.
  • A grid that 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 grid will be a list of lists.
  • The inner lists (rows) of the grid will have consistent lengths.
  • The barriers will be a list of tuples.
  • Each tuple in barriers will contain two integers representing (row, col).
  • The number of rows in the grid will be between 0 and 100.
  • The number of columns in the grid will be between 0 and 100.
  • The number of barriers in the barriers list 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 None as it modifies the grid in-place.
Loading editor...
python