#K49057. Maximum Trees Through the Forest
Maximum Trees Through the Forest
Maximum Trees Through the Forest
You are given an n × m grid, where each cell is either empty (represented by .
) or contains a tree (represented by T
). A lumberjack starts at the top‐left corner (cell (1,1)) and needs to reach the bottom‐right corner (cell (n,m)) by only moving right or down.
Your task is to compute the maximum number of trees the lumberjack can pass through along any valid path. This problem can be solved using dynamic programming. Specifically, if we define dp[i][j] as the maximum trees encountered from the start to cell (i,j), then the recurrence relation is given by:
\( dp[i][j] = \max(dp[i-1][j], dp[i][j-1]) + \delta \)
where \( \delta = 1 \) if the cell at (i,j) contains a tree, and 0 otherwise.
inputFormat
The first line contains two integers n and m indicating the number of rows and columns respectively. This is followed by n lines, each containing a string of length m representing the grid. Each character is either .
(empty cell) or T
(tree).
Input is provided via standard input (stdin).
outputFormat
Output a single integer: the maximum number of trees that the lumberjack can pass through on a valid path from the top-left corner to the bottom-right corner. Output should be printed to standard output (stdout).
## sample3 4
.T..
TT.T
..T.
3
</p>