#C7215. Maximum Treasures in a Maze
Maximum Treasures in a Maze
Maximum Treasures in a Maze
You are given a maze in the form of a grid with N rows and M columns. Each cell in the maze can be one of the following characters:
'T'
: A treasure.'O'
: An obstacle that cannot be entered.'.'
: An empty cell.
Your task is to determine the maximum number of treasures you can collect by starting at the top-left corner (cell (1,1)) of the maze and moving to adjacent cells in the four cardinal directions (up, down, left, right). Movement into a cell is only possible if the cell is not an obstacle and has not been visited already.
Note: If the starting cell is an obstacle, the answer is 0. The connectivity is defined by adjacent moves only.
The problem can be mathematically understood as finding the number of cells marked T in the connected component containing the cell (1,1), provided that the maze cell (1,1) is not blocked. In mathematical notation, if we denote the grid by \(G\) and the starting cell by \(G(0,0)\) (using 0-indexing), then the answer is the sum of treasures in the connected region reachable from \(G(0,0)\).
inputFormat
The first line contains two space-separated integers N and M \((1 \leq N, M \leq 100)\) representing the number of rows and columns respectively. The next N lines each contain a string of M characters, representing the maze.
outputFormat
Output a single integer representing the maximum number of treasures that can be collected starting from the top-left corner of the maze.
## sample5 5
T.T..
.OO..
.T.T.
.OO..
.....
4