您的位置:首页 > 其它

#第一周1003解题报告#

2015-08-02 12:57 549 查看
Problem Description

A biologist experimenting with DNA modification of bacteria has found a way to make bacterial colonies sensitive to the

surrounding population density. By changing the DNA, he is able to “program” the bacteria to respond to the varying densities in their immediate neighborhood.

The culture dish is a square, divided into 400 smaller squares (20x20). Population in each small square is measured on a four point scale (from 0 to 3). The DNA information is represented as an array D, indexed from 0 to 15, of integer values and is interpreted as follows:

In any given culture dish square, let K be the sum of that square’s density and the densities of the four squares immediately to the left, right, above and below that square (squares outside the dish are considered to have density 0). Then, by the next day, that dish square’s density will change by D[K] (which may be a positive, negative, or zero value). The total density cannot, however, exceed 3 nor drop below 0.

Now, clearly, some DNA programs cause all the bacteria to die off (e.g., [-3, -3, …, -3]). Others result in immediate population explosions (e.g., [3,3,3, …, 3]), and others are just plain boring (e.g., [0, 0, … 0]). The biologist is interested in how some of the less obvious DNA programs might behave.

Write a program to simulate the culture growth, reading in the number of days to be simulated, the DNA rules, and the initial population densities of the dish.

Input

Input to this program consists of three parts:

The first line will contain a single integer denoting the number of days to be simulated.

The second line will contain the DNA rule D as 16 integer values, ordered from D[0] to D[15], separated from one another by one or more blanks. Each integer will be in the range -3…3, inclusive.

The remaining twenty lines of input will describe the initial population density in the culture dish. Each line describes one row of squares in the culture dish, and will contain 20 integers in the range 0…3, separated from one another by 1 or more blanks.

Output

The program will produce exactly 20 lines of output, describing the population densities in the culture dish at the end of the simulation. Each line represents a row of squares in the culture dish, and will consist of 20 characters, plus the usual end-of-line terminator.

Each character will represent the population density at a single dish square, as follows:

No other characters may appear in the output.

Sample Input

1

2

0 1 1 1 2 1 0 -1 -1 -1 -2 -2 -3 -3 -3 -3

3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output

!……………..

!………………

!……………….

………………..

………………..

………………..

………………..

………!……….

……..!#!………

…….!#X#!……..

……..!#!………

………!……….

………………..

………………..

………………..

………………..

………………..

………………..

………………..

………………..

题意是有20*20的格子里面有细菌,细菌们每天变化一次,变化规则由D[k]指定,k的计算方式如下

d[ square[m][n - 1]+square[m][n + 1] + square[m - 1][n]+square[m + 1][n]+square[m][n]];


即上下左右加他本身,使用temp【22】【22】作为缓存数组,一天变化后复制给细菌格子,使得当天变化的细菌之间不会相互影响,声明数组为【22】【22】将边界均置为0;方便处理边界元素,细菌的值将被强制控制在0-3,超出3,置为3,低于0,置为0;最终输出时将0-3转化为!#X点即可。

java代码:

import java.util.Scanner;

public class Main1003 {
public static void main(String args[]) {
int f;
int day;//天数
int d[] = new int[16];
int[][] square = new int[22][22];//细菌格子
int[][] temp = new int[22][22];//缓存变化结果,一天结束后赋给细菌格子
for (int i = 0; i < 22; i++)
for (int j = 0; j < 22; j++) {//边界初始化为0
square[i][j] = 0;
}
for (int m = 0; m < 22; m++)
for (int n = 0; n < 22; n++) {
square[m]
= temp[m]
;
}
Scanner sc = new Scanner(System.in);
f = sc.nextInt();
for(int a = 0;a<f;a++){

day = sc.nextInt();
for (int i = 0; i < 16; i++) {
d[i] = sc.nextInt();
}
for (int i = 1; i <= 20; i++) {//读入细菌格子初始化值
for (int j = 1; j <= 20; j++) {
square[i][j] = sc.nextInt();
}
}
for (int i = 0; i < day; i++) {
for (int m = 1; m <= 20; m++) {
for (int n = 1; n <= 20; n++) {
temp[m]
= square[m]
+d[ square[m][n - 1]
+ square[m][n + 1] + square[m - 1]

+ square[m + 1]
+square[m]
];

if (temp[m]
>= 3) {
temp[m]
= 3;
}
else if (temp[m]
<= 0) {
temp[m]
= 0;
}
}
}

for (int m = 1; m <= 20; m++) {
for (int n = 1; n <= 20; n++) {
square[m]
= temp[m]
;
}
}
}

for (int i = 1; i <= 20; i++) {
for (int j = 1; j <= 20; j++) {
if (square[i][j] == 0)
System.out.print('.');
else if (square[i][j] == 1) {
System.out.print('!');
} else if (square[i][j] == 2) {
System.out.print('X');
} else if (square[i][j] == 3) {
System.out.print('#');
}

}
System.out.println();
}
if(a<f-1)
System.out.println();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: