Test instructions
A biologist experimenting with DNA modification of bacteria have found a-by-make bacterial colonies sensitive to the
Surrounding population density. By changing the DNA, he's able to "program" the bacteria to respond to the varying densities in their immediate neighborh Ood.
A biologist experimented with modifying the DNA of the bacteria to make the bacteria affected by the density of the bacteria around it. By modifying the DNA, he can "program" bacteria to react to the density changes in their neighbors.
The culture dish is a square, divided into smaller squares (20x20). Population in each small square was measured on a four point scale (from 0 to 3). The DNA information is represented as a array D, indexed from 0 to $, of the integer values and is interpreted as follows:
The petri dish is square, divided into 400 smaller squares. The number of bacteria per small block is represented by 0 to 34 numbers. The DNA information is placed in an array d, and the array designator is from 0 to 15. The following explanation:
In any given culture dish square, let K is the sum of that square's density and the densities of the four squares immediat Ely to the left, right, above and below that square (squares outside the dish is considered to the density 0). Then, by the next day, that Dish square ' s density would change by d[k] (which could be a positive, negative, or zero value). The total density cannot, however, exceed 3 nor drop below 0.
In a given small square of a petri dish, K is the density of the small square, and the density of the four small squares adjacent to it (the density outside the petri dish is considered to be 0). Then, the next day, the density of this small square will change according to D[k] (D[k] may be positive, negative or 0). But the density of the small squares will not be more than 3 or less than 0 in the end.
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 on how to some of the less obvious DNA programs might behave.
It is now clear that some DNA programs cause bacteria to die (e.g. [-3,-3, ...,-3]). There are also a number of bacterial surges (e.g. [3,3,3, ..., 3]), others that do not cause any changes (e.g. [0,0,0, ..., 0]). Biologists want to know how some of the not-so-obvious DNA programs will affect them.
Write a program to simulate the culture growth, reading in the number of days to be simulated, the DNA rules, and the Init Ial population densities of the dish.
Write a program that simulates these changes, reading the number of days that need to be simulated, DNA rules, and the initial density of bacteria in the petri dish.
Idea: This problem card for a long time, originally in their own computer test how to measure, but submit is wrong answer. Go a lot of detours, or did not succeed, search the answer on the Internet. It's right to see your own way of thinking. But the people are very clever in the calculation of K, in my program I use SUM, that is, five small squares of density and. Originally I only used the 20*20 array to put the density of the small squares. So the calculation of the time need to consider the side and the small square in the corner, in my program to calculate sum divided into i=0,j!=0 and j<19, and many other situations. At that time also thought that I thought carefully, but saw that the author directly uses the 22*22 array, so the edge and the angle does not have to consider separately, by this commits, accepted! Maybe I have too many of the points, I do not know where there are loopholes ... So the ACM question should be considered well .....
The author's link http://blog.csdn.net/wankaiming/article/details/8104857 the C + + that he used
Import Java.util.Scanner;
public class ANewGrowth1051 {
static int sum = 0;//the sum of that square ' s density and the densities of
The four squares immediately to the left, right,
Above and below that square
Static int[][] ini = new int[22][22];//density in each small square
static int num = 1;//The number of times a simulation is required, default is 1
static int days = 0;//to simulate
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int[] Arrayd = new int[16];//DNA information
num = Sc.nextint ();
for (int i = 0; i < num; i++) {
Days = Sc.nextint ();//Read in
for (int j = 0; J <; J + +)
Read into DNA information
ARRAYD[J] = Sc.nextint ();
for (int k = 1; k <; k++)
Read initial density
for (int m = 1; m <; m++)
Ini[k][m] = Sc.nextint ();
for (int z = 0, z < days; z++) {
Calculate (Arrayd);
}
for (int x = 1; x < + × x + +) {
for (int y = 1; y < y++) {
if (ini[x][y] = = 0)
System.out.print (".");
else if (ini[x][y] = = 1)
System.out.print ("!");
else if (ini[x][y] = = 2)
System.out.print ("X");
else if (ini[x][y] = = 3)
System.out.print ("#");
}
System.out.println ();
}
if (I < num-1)
System.out.println ();
}
}
public static void Calculate (int[] arrayd) {
Int[][] then = new INT[22][22];
for (int i = 1; i <; i++) {
for (int j = 1; J <; J + +) {
sum = ini[i-1][j] + ini[i + 1][j] + ini[i][j-1]
+ ini[i][j + 1] + ini[i][j];//calculate sum
THEN[I][J] = Ini[i][j] + arrayd[sum];//use then[][] because the subsequent calculations need to use the previous density, so after the calculation can not immediately change the density value, first put in then inside
if (Then[i][j] < 0)//must not be less than 0 or more than 3
THEN[I][J] = 0;
if (Then[i][j] > 3)
THEN[I][J] = 3;
}
}
INI = then;
}
}
1051A New Growth Industry ACM Answer Java edition