#K48632. Garden Growth

    ID: 28464 Type: Default 1000ms 256MiB

Garden Growth

Garden Growth

In this problem, you are given a rectangular garden grid represented by n rows and m columns. Each cell in the grid is one of the following characters:

  • . : an empty cell
  • P : a seed
  • # : an obstacle

A seed (P) will grow into a plant (G) if and only if at least one of its adjacent cells (up, down, left, or right) is empty (i.e. contains a .). Otherwise, the seed remains unchanged.

Formally, for every seed located at cell \( (i,j) \), if there exists an adjacent cell \( (i',j') \) such that \(|i-i'|+|j-j'|=1\) and the cell \( (i',j') \) contains ., then the seed transforms into a plant G.

Your task is to process the grid accordingly and output the updated garden grid.

inputFormat

The first line of input contains two integers n and m which denote the number of rows and columns, respectively. This is followed by n lines, each containing a string of length m representing a row of the garden grid.

For example:

5 5
.....
..P..
.###.
..P..
.....

outputFormat

Output n lines, each containing the updated garden grid after converting seeds that can grow into plants (PG).

For example:

.....
..G..
.###.
..G..
.....
## sample
5 5
.....
..P..
.###.
..P..
.....
.....

..G.. .###. ..G.. .....

</p>