Warring Civilizations


I encountered this problem in an Online Programming Competitions conducted by MIT. I thought I might post this over here…. It was categorized as a Beginner level problem and finding the solution was also pretty straight forward! So try to solve it…..

Danny has created a new civilization on a 2D grid. At the outset each grid location may be  occupied by one of three life forms: Woods, Axe, or Grass. Each day, differing life forms  occupying horizontally or vertically adjacent grid locations wage war. In each war, Woods  always defeat Axe, Axe always defeat Grass, and Grass always defeat Woods. At the end of the day, the winner expands its territory to include the loser’s grid position. The loser vacates the  position.Determine the territory occupied by each life form after n days.

Input

The first line of input contains t, the number of test cases. Each test case begins with  three integers not greater than 100: r and c, the number of rows and columns in the grid,  and n. The grid is represented by the r lines that follow, each with c characters. Each character in the grid is W, A, or G, indicating that it is occupied by Woods, Axe, or Grass respectively.

Output For each test case, print the grid as it appears at the end of the nth day.

Example Input:

2

3 3 1

WWW

WAW

WWW

3 4 2

WAGW

AGWA

GWAG

Output:

WWW

WWW

WWW

WWWA

WWAG

WAGW

I actually save copies of the problems that I find very interesting and challenging in the OPCs that I participate in. Ill post them here one by one.

Regards..

Steve Robinson