#K86342. Traffic Lights Simulation
Traffic Lights Simulation
Traffic Lights Simulation
You are given an M x N grid representing traffic lights. Each cell in the grid is either 'G' (green) or 'R' (red). The grid evolves in discrete steps. At each step, the state of each cell is updated based on its four direct neighbors (up, down, left, right) according to the following rules:
- If the current cell is green (G), then it remains green if and only if \[ \text{(\# of green neighbors)} > 0 \text{ and } \text{(\# of red neighbors)} = 0 \] ; otherwise, it becomes red (R).
- If the current cell is red (R), then it remains red if and only if \[ \text{(\# of red neighbors)} > 0 \text{ and } \text{(\# of green neighbors)} = 0 \] ; otherwise, it becomes green (G).
Your task is to simulate the evolution of the grid for K steps. Output the final configuration of the grid.
inputFormat
The first line of input contains three integers M, N, and K separated by spaces where:
- M is the number of rows.
- N is the number of columns.
- K is the number of steps to simulate.
This is followed by M lines, each containing a string of length N that represents a row of the grid. Each character is either 'G' or 'R'.
outputFormat
Output the final configuration of the grid after simulating K steps. Each of the M lines should be a string of length N representing a row of the grid.
## sample3 3 1
GGG
GRG
GGG
GRG
RGR
GRG
</p>