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 Format:
Input to this program consists of three parts:
1. The first line will contain a single integer denoting the number of days to be simulated.
2. 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.
3. 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 Format:
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
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#!........ ........!#!......... .........!.......... .................... .................... .................... .................... .................... .................... .................... ....................
题意:这题还真考验英语能力。。反正我题目是看了N遍还看了别人的一些理解才理解题目。大概说明下意思,先输入n表示测试数据的组数。然后正式的输入数据开始了 然后输入一个数,在我的代码里面是days表示培养细菌的天数。再输入DNA信息,这里我用了数组d[16],然后是20*20的矩阵,用a[22][22]表示(我这么表示是免去了考虑边界溢出)。每个a[i][j]的值就是该位置培养皿的细菌密度,题目意思就是a[i][j]位置的培养皿的细菌密度受到周围4个位置的培养皿的影响,经过一天后,该位置培养皿的密度是本身原来密度加上4个边上的培养皿密度。最后计算完每个培养皿经i天后的密度,再用相应的符号代替培养皿密度的值即可
思路:这里注意几点。关键是题意的理解。以下说几个易错点再说思路。
1.一天后相应位置的密度是在原来自己的基础上加上四周的。
2.不要忘记进行第二组数据的测试前要对之前的变量再初始化(这个错误我找了2个小时多。。郁闷死)
3.必要变量重名
4.不要算好一个培养皿下一天的密度就是直接替换上去覆盖了原来的,会出错!(因为原来的密度会被周围的培养皿计算下一天的密度用到)
5.两组数据之间要有空行
6.最后一组数据输出结束后不用空行
我的思路是定义a[22][22]数组 初始化为0,然后根据题目的计算要求将下一天的密度记入到k[i][j]中,我定义的k[22][22]分别对应a[22][22]中的每个培养皿,然后一天的都计算完了,再将k[i][j]的值全部赋值给a[i][j].
其实理解了题意做起来还是很简单的.
Sample Program Here
#include<iostream> using namespace std; int main(){ int a[22][22]; char b[22][22];//保存替换字符后的值 int days,n,i,j,k[22][22]; //days是天数,n是测试的组数,i,j是数组的参数,k是题目中的k int d[16]; cin>>n; for(int t=0;t<n;t++){ cin>>days; //初始化 int a[22][22]={0}; char b[22][22]={0}; //初始化DNA序列 for(i=0;i<16;i++) cin>>d[i]; //初始化之后的20行 for(i=1;i<21;i++) for(j=1;j<21;j++) cin>>a[i][j]; for(int day=0;day<days;day++){ //天数的循环 //第d天的变化 for(i=1;i<21;i++) for(j=1;j<21;j++){ k[i][j]=a[i+1][j]+a[i][j+1]+a[i-1][j]+a[i][j-1]+a[i][j];//增加包括自己以内的上下左右的值,每个a[i][j]对应一个k值所以K也弄成二维数组 } //要全部统计好再完成替换,直接算好替换会出错(因为利用的是新的值,原来的值被覆盖) for(i=1;i<21;i++) for(j=1;j<21;j++){ a[i][j]+=d[k[i][j]]; //注意是by所以是再原有基础上增加 if(a[i][j]>3) a[i][j]=3; else if(a[i][j]<0) a[i][j]=0; } } //进行符号的替换 for(i=0;i<22;i++) for(j=0;j<22;j++){ if(a[i][j]==0) b[i][j]='.'; else if(a[i][j]==1) b[i][j]='!'; else if(a[i][j]==2) b[i][j]='X'; else b[i][j]='#'; } //输出结果 for(i=1;i<21;i++){ for(j=1;j<21;j++){ cout<<b[i][j]; } cout<<endl; } if(t<n-1) cout<<endl; }//参数为t的for循环结束 return 0; }