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.


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:


3 3 1




3 4 2











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.


Steve Robinson

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s