Loading problem...
You are developing a grid-based exploration system for navigating a dangerous minefield. The field is represented as an m × n character matrix called grid, where each cell contains one of the following markers:
'M' (Mine Hazard): Represents a concealed explosive device that has not yet been triggered.'E' (Empty/Unexplored): Represents a safe cell that has not been examined yet.'B' (Blank/Safe Zone): Represents a revealed cell with no adjacent hazards (including horizontal, vertical, and all four diagonal neighbors).'1' to '8': Represents a revealed cell showing the count of adjacent hazards surrounding it.'X' (Exploded/Triggered): Represents a hazard that has been activated (game over scenario).You are also given a position array containing two integers [row, col] representing the coordinates of the cell the explorer chooses to probe. This cell is guaranteed to be either 'M' (Mine Hazard) or 'E' (Empty/Unexplored).
Your task is to return the updated grid after processing the probe action according to these exploration protocols:
Hazard Detection: If the probed cell contains a hidden hazard 'M', the exploration fails. Mark this cell as 'X' (Exploded/Triggered) and return the grid immediately.
Safe Cascade: If the probed cell is unexplored 'E' and has zero adjacent hazards, change it to 'B' (Blank/Safe Zone). Then, automatically probe all eight adjacent unexplored cells recursively, creating a cascade effect that reveals connected safe regions.
Boundary Cell: If the probed cell is unexplored 'E' and has one or more adjacent hazards, change it to a digit character ('1' through '8') representing the exact count of neighboring hazards. This cell acts as a boundary and does not trigger further exploration.
Termination: Return the final grid state when no more cells can be revealed through the cascade process.
The adjacency relationship includes all eight directions: up, down, left, right, and the four diagonals.
grid = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]]
position = [3,0][["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]The probe starts at position [3,0]. Since this cell has no adjacent hazards, it becomes 'S' (shown as 'B' in output). The cascade spreads outward, revealing all connected safe cells. Cells adjacent to the hazard 'M' display digit counts ('1' in this case), forming a boundary around the hidden danger. The hazard at [1,2] remains concealed.
grid = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
position = [1,2][["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]The explorer probes the cell at [1,2], which contains a hidden hazard 'M'. This triggers the hazard, marking it as 'X' (Triggered). The exploration ends immediately with a failed outcome.
grid = [["E","M","E"],["E","E","E"],["E","E","E"]]
position = [0,0][["1","M","E"],["E","E","E"],["E","E","E"]]The probe at [0,0] detects one adjacent hazard at [0,1]. The cell is marked with '1' to indicate this count. Since there's at least one adjacent hazard, no cascade occurs, and only this single cell is revealed.
Constraints