CodeGolf: Brothers

Posted by John McClane on Stack Overflow See other posts from Stack Overflow or by John McClane
Published on 2009-10-25T17:27:45Z Indexed on 2010/05/31 14:33 UTC
Read the original article Hit count: 457

Hi guys, I just finished participating in the 2009 ACM ICPC Programming Conest in the Latinamerican Finals. These questions were for Brazil, Bolivia, Chile, etc.

My team and I could only finish two questions out of the eleven (not bad I think for the first try).

Here's one we could finish. I'm curious to seeing any variations to the code. The question in full: ps: These questions can also be found on the official ICPC website available to everyone.


In the land of ACM ruled a greeat king who became obsessed with order. The kingdom had a rectangular form, and the king divided the territory into a grid of small rectangular counties. Before dying the king distributed the counties among his sons.

The king was unaware of the rivalries between his sons: The first heir hated the second but not the rest, the second hated the third but not the rest, and so on...Finally, the last heir hated the first heir, but not the other heirs.

As soon as the king died, the strange rivaly among the King's sons sparked off a generalized war in the kingdom. Attacks only took place between pairs of adjacent counties (adjacent counties are those that share one vertical or horizontal border). A county X attacked an adjacent county Y whenever X hated Y. The attacked county was always conquered. All attacks where carried out simultanously and a set of simultanous attacks was called a battle. After a certain number of battles, the surviving sons made a truce and never battled again.

For example if the king had three sons, named 0, 1 and 2, the figure below shows what happens in the first battle for a given initial land distribution:

alt text


INPUT

The input contains several test cases. The first line of a test case contains four integers, N, R, C and K.

  1. N - The number of heirs (2 <= N <= 100)
  2. R and C - The dimensions of the land. (2 <= R,C <= 100)
  3. K - Number of battles that are going to take place. (1 <= K <= 100)

Heirs are identified by sequential integers starting from zero. Each of the next R lines contains C integers HeirIdentificationNumber (saying what heir owns this land) separated by single spaces. This is to layout the initial land.

The last test case is a line separated by four zeroes separated by single spaces. (To exit the program so to speak)


Output

For each test case your program must print R lines with C integers each, separated by single spaces in the same format as the input, representing the land distribution after all battles.


Sample Input:                          Sample Output:
3 4 4 3                                2 2 2 0
0 1 2 0                                2 1 0 1 
1 0 2 0                                2 2 2 0
0 1 2 0                                0 2 0 0 
0 1 2 2

Another example:

Sample Input:                          Sample Output:
4 2 3 4                                1 0 3
1 0 3                                  2 1 2
2 1 2

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about console-application