Battle

来源:互联网 发布:淘宝秒杀方法 编辑:程序博客网 时间:2024/04/30 09:11

Problem Description

  In the land of League of Legends ruled some kingdoms.The land had a rectangular form and each kingdom was a sub-rectangle.These kingdoms had developed a strange rivalry: the first kingdom hated the second kingdom, but not the rest; the second kingdom hated the third kingdom, but not the rest, and so on ...Finally, the last kingdom hated the first kingdom, but not the other heirs.
  One day, these kingdoms sparked off a generalized war in the land. Attacks only took place between pairs of adjacent kingdoms (adjacent kingdoms are those that share one vertical or horizontal border). A kingdom X attacked an adjacent kingdom Y whenever X hated Y. The attacked kingdom was always conquered by the attacking kingdom. 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 kingdoms made a truce and never battled again.
  For example, if the land had three kingdoms, named 0, 1 and 2, the figure below shows what happens in the first battle for a given initial land distribution:
    0 0 1      0 0 0
    0 1 1   -> 0 0 1
    0 2 2      2 1 1
  You were hired to help an League of Legends historian determining, given the number of kingdoms, 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 kingdoms (2<=N<=100), R and C are the dimensions of the land (2<=R, C<=100), and K is the number of battles (1<=K<=100). kingdoms are identified by sequential integers starting from zero (0 is the first kingdom, 1 is the second kingdom, ..., N - 1 is the last kingdom). Each of the next R lines contains C integers H(r,c) separated by single spaces, representing initial land distribution: H(r,c) is the initial owner of the land in row r and column c (0<=H(r,c)<=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 30 1 2 01 0 2 00 1 2 00 1 2 24 2 3 41 0 3 2 1 2 8 4 2 10 7 1 6 2 5 3 4 0 0 0 0

Sample Output

2 2 2 02 1 0 12 2 2 00 2 0 01 0 3 2 1 2 7 6 0 5 1 4 2 3
题目本身不难,但是要注意结果与原值的保存。
#include <iostream>#include <cstring>#include <cstdio>using namespace std;int n,r,c,k;int st[2][101][101];//当做两个二维数组使用,分别存储相邻两次的结果int visi[101][101];//表示当前坐标上是否战争int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, 1, 0, -1};//两个数组组合完成对目标坐标上下左右的取址bool suit(int a,int b){//判断坐标是否在标定领土范围内if(a >= 0 && a < r && b >= 0 && b < c)return true;return false;}int main(){while(cin >> n >> r >> c >> k && (n != 0||r != 0||c != 0||k != 0)){for(int i = 0;i < r;i++)for(int j = 0;j <c;j++)cin >> st[0][i][j];int biao;//用来切换两个数表biao = 0;for(int p =1;p <= k;p++){memset(visi,0,sizeof(visi));//初始化标识数表for(int i = 0;i < r;i ++)for(int j = 0;j < c;j ++){for(int jj = 0;jj < 4;jj ++){
                                        //分别判定坐标的上下左右相邻位置是否发生战争int a1 = i + dx[jj];int b1 = j + dy[jj];if(suit(a1,b1)){if((st[biao][i][j]+1)%n == st[biao][a1][b1]){st[!biao][a1][b1] = st[biao][i][j];visi[a1][b1] = 1;}}}}for(int i = 0;i < r;i ++)for(int j = 0;j < c;j ++)if(!visi[i][j])st[!biao][i][j] = st[biao][i][j];//将未战争的坐标也挪到邻表中,完成一次全局战争biao = !biao;}for(int i = 0;i < r;i ++){for(int j = 0;j < c-1;j ++)cout << st[biao][i][j] << " ";cout << st[biao][i][c-1] << endl;}}return 0;}
原创粉丝点击