24h購物| | PChome| 登入
2013-07-18 10:29:40| 人氣1,187| 回應0 | 上一篇 | 下一篇

[UVA][生命遊戲] 12187 - Brothers

推薦 0 收藏 0 轉貼0 訂閱站台

In the land of ACM ruled a great 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.

However, he was unaware that his children had developed a strange rivalry: the first heir hated the second heir, but not the rest; the second heir hated the third heir, 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 rivalry 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 the owner of X hated the owner of Y. The attacked county was always conquered by the attacking brother. By a rule of honor all the attacks were carried out simultaneously, and a set of simultaneous 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:

epsfbox{p4473.eps}

You were hired to help an ACM historian determining, given the number of heirs, the initial land distribution and the number of battles, what was the land distribution after all battles.

Input 

The input contains several test cases. The first line of a test case contains four integers N, R, C and K, separated by single spaces. N is the number of heirs (2$ le$N$ le$100), R and C are the dimensions of the kingdom (2$ le$R, C$ le$100), and K is the number of battles (1$ le$K$ le$100). Heirs are identified by sequential integers starting from zero (0 is the first heir, 1 is the second heir, ..., N - 1 is the last heir). Each of the next R lines contains C integers Hr, c separated by single spaces, representing initial land distribution: Hr, c is the initial owner of the county in row r and column c (0$ le$Hr, c$ le$N - 1).

The last test case is followed by a line containing four zeroes separated by single spaces.

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 

3 4 4 3
0 1 2 0
1 0 2 0
0 1 2 0
0 1 2 2
4 2 3 4
1 0 3 
2 1 2 
8 4 2 1
0 7 
1 6 
2 5 
3 4 
0 0 0 0

Sample Output 

2 2 2 0
2 1 0 1
2 2 2 0
0 2 0 0
1 0 3 
2 1 2 
7 6 
0 5 
1 4 
2 3

兄弟們互相仇視,關係如一個環狀,每隔一個時間點勢力會被占據,問多少時間後的勢力情況。


#include <stdio.h>
#include <string.h>

int main() {
int N, R, C, K;
while(scanf("%d %d %d %d", &N, &R, &C, &K) == 4) {
if(N+R+C+K == 0)
break;
int g[2][105][105];
int i, j, k;
for(i = 0; i < R; i++)
for(j = 0; j < C; j++)
scanf("%d", &g[0][i][j]);
int flag = 1;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
while(K--) {
for(i = 0; i < R; i++) {
for(j = 0; j < C; j++) {
g[flag][i][j] = g[!flag][i][j];
int x = g[!flag][i][j]-1;
if(x < 0) x += N;
for(k = 0; k < 4; k++) {
int tx = i+dx[k], ty = j+dy[k];
if(tx < 0 || ty < 0 || tx >= R || ty >= C)
continue;
if(g[!flag][tx][ty] == x) {
g[flag][i][j] = x;
break;
}
}
}
}
flag = 1-flag;
}
flag = 1-flag;
for(i = 0; i < R; i++, puts("")) {
for(j = 0; j < C; j++) {
if(j) putchar(' ');
printf("%d", g[flag][i][j]);
}
}
}
return 0;
}
 

台長: Morris
人氣(1,187) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 12291 - Polyomino Composer
此分類上一篇:[UVA] 230 - Borrowers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文