Rock, Scissors, Paper

Time limit1sMemory limit128 MB

Summary
Given a grid of Rock, Scissors, and Paper cells, simulate simultaneous battles each day and print the grid after n days.
Level

Medium5 of 10

Topics
Simulation, Matrix, Implementation
Solved
No attempts yet

Problem

Bart's sister Lisa has built a new civilization on a two-dimensional grid. At the start, each grid cell may be occupied by one of three life forms: Rock, Scissors, or Paper. Each day, whenever two horizontally or vertically adjacent cells hold different life forms, they wage war. In every war Rock always beats Scissors, Scissors always beats Paper, and Paper always beats Rock. At the end of the day the winner expands into the loser's cell and takes it over, while the loser disappears from that cell. All of the wars on a given day happen simultaneously.

Determine the territory occupied by each life form after nn days.

Input

The first line contains tt, the number of test cases. Each test case begins with three integers rr, cc, and nn: the number of rows, the number of columns, and the number of days that pass. Each of these integers is at most 100. The next rr lines describe the grid, each containing cc characters. Every character is R, S, or P, meaning that the cell is occupied by Rock, Scissors, or Paper respectively.

Output

For each test case, print the grid as it appears at the end of day nn. Print one empty line between the outputs of successive test cases.

Examples7

  1. Example 1

    Input
    2
    3 3 1
    RRR
    RSR
    RRR
    3 4 2
    RSPR
    SPRS
    PRSP
    
    Expected output
    RRR
    RRR
    RRR
    
    RRRS
    RRSP
    RSPR
    
  2. Example 2

    Input
    1
    1 1 5
    P
    
    Expected output
    P
    
  3. Example 3

    Input
    1
    2 2 0
    RS
    PR
    
    Expected output
    RS
    PR
    
  4. Example 4

    Input
    1
    2 3 10
    SSS
    SSS
    
    Expected output
    SSS
    SSS
    
  5. Example 5

    Input
    2
    1 3 1
    RSP
    2 2 1
    RP
    SR
    
    Expected output
    RRS
    
    PP
    RP
    
  6. Example 6

    Input
    1
    1 7 3
    RSPRSPR
    
    Expected output
    RRRRSPR
    
  7. Example 7

    Input
    1
    6 1 4
    R
    S
    P
    R
    S
    P
    
    Expected output
    R
    R
    R
    R
    R
    S