You are given a 2D grid composed of different characters, representing a maze. Each character in the grid represents a cell that can either be open or blocked. You need to determine the characters that are trapped in the maze.
A character is considered trapped if it is surrounded by 'X' on all four sides (up, down, left, right). If a character 'A' is adjacent to another character 'B' (up, down, left, or right), they form a group and will not be considered trapped.
You need to find and list all the characters that are trapped along with their positions in the grid.
char row column on a new line.Input:
5 8
XXXXXXAX
XAABXXXD
XAAXXXAX
XXAXXCX
XXXXXXXX
Output:
2
A 0 6
D 1 7
Explanation: The input grid represents a 5x8 grid where 'X' denotes obstacles and letters represent characters. The algorithm identifies groups of connected characters not separated by obstacles and checks if they are entirely surrounded by 'X'. In this case, characters 'A' and 'D' are identified as trapped, resulting in the output showing the number of trapped characters and their positions.
Input:
6 6
XXXXXX
XAAAXX
XAXXXX
XAXXAX
XAXXXX
XXXXXX
Output:
1
A 3 4
Explanation: The input grid represents a 6x6 grid with 'X' as obstacles and 'A' as characters. The algorithm identifies groups of connected 'A' characters and checks if they are fully enclosed by 'X'. In this case, only one group of 'A' characters is completely surrounded by obstacles, specifically 'A' at position (3, 4).