UVa 457 - Linear Cellular Automata
Linear Cellular Automata |
Time limit: 3.000 seconds
A biologist is experimenting with DNA modification of bacterial colonies being grown in a linear array of culture dishes. By changing the DNA, he is able ``program" the bacteria to respond to the population density of the neighboring dishes. Population is measured on a four point scale (from 0 to 3). The DNA information is represented as an array DNA, indexed from 0 to 9, of population density values and is interpreted as follows:
- In any given culture dish, let K be the sum of that culture dish's density and the densities of the dish immediately to the left and the dish immediately to the right. Then, by the next day, that dish will have a population density of DNA[K].
- The dish at the far left of the line is considered to have a left neighbor with population density 0.
- The dish at the far right of the line is considered to have a right neighbor with population density 0.
Now, clearly, some DNA programs cause all the bacteria to die off (e.g., [0,0,0,0,0,0,0,0,0,0]). Others result in immediate population explosions (e.g., [3,3,3,3,3,3,3,3,3,3]). The biologist is interested in how some of the less obvious intermediate DNA programs might behave.
Write a program to simulate the culture growth in a line of 40 dishes, assuming that dish 20 starts with a population density of 1 and all other dishes start with a population density of 0.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
For each input set your program will read in the DNA program (10 integer values) on one line.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
For each input set it should print the densities of the 40 dishes for each of the next 50 days. Each day's printout should occupy one line of 40 characters. Each dish is represented by a single character on that line. Zero population densities are to be printed as the character ` '. Population density 1 will be printed as the character `.'. Population density 2 will be printed as the character `x'. Population density 3 will be printed as the character `W'.
Sample Input
1 0 1 2 0 1 3 3 2 3 0
Sample Output
bbbbbbbbbbbbbbbbbbb.bbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbb.xbx.bbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbb.bb.bb.bbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbb.........bbbbbbbbbbbbbbbb bbbbbbbbbbbbbb.xbbbbbbbx.bbbbbbbbbbbbbbb bbbbbbbbbbbbb.bbxbbbbbxbb.bbbbbbbbbbbbbb bbbbbbbbbbbb...xxxbbbxxx...bbbbbbbbbbbbb bbbbbbbbbbb.xb.WW.xbx.WW.bx.bbbbbbbbbbbb bbbbbbbbbb.bbb.xxWb.bWxx.bbb.bbbbbbbbbbb
Note: Whe show only the first ten lines of output (the total number of lines must be 50) and the spaces have been replaced with the character "b" for ease of reading. The actual output file will use the ASCII-space character, not "b".
#include<stdio.h> #include<string.h> int is_char(int w[], int t) { int flag = w[t]; switch(flag) { case 0: printf(" "); break; case 1: printf("."); break; case 2: printf("x"); break; case 3: printf("W"); break; default: return -1; } } int main() { int dish[42], DNA[10], dishcpy[42], i, j, n, midl, t, data; scanf("%d", &n); for(t=1; t<=n; ++t) { memset(dish, 0, sizeof(dish)); memset(dishcpy, 0, sizeof(dish)); dishcpy[20] = dish[20] = 1; dish[0] = 0; memset(DNA, 0, sizeof(DNA)); for(i=0; i<10; ++i) scanf("%d", &DNA[i]); for(data=1; data<=50; ++data) { for(j=1; j<=40; ++j) is_char(dishcpy, j); printf("\n"); for(midl=1; midl<=40; ++midl) { dishcpy[midl] = DNA[(dish[midl-1]+dish[midl]+dish[midl+1])]; } for(j=1; j<=40; ++j) dish[j]=dishcpy[j]; } if(t != n) printf("\n"); } return 0; }
解题报告:
一开始题目长且对题目所指的意思不太明白,DNA序列是你要输入的数据,所指dish的值=DNA中第K个数的值=所指dish细菌浓度+所指dish左边细菌浓度+所指dish右边细菌浓度(0—40)tip:细菌的浓度(0-3表示),最左边的dish假设还有一个浓度为0的dish,同理右边也是如此假设。输出每天dish值的变化,共50天;input中要求输入要输入空格,可以忽略,正是以为要自己输出blank space弄得我WA了一次,这点教训要谨记!
一开始看不懂follows中的far left has left,忽略后做出来的题目肯定得WA了, 二就是对input表示不理解,换行是否要自己输入还是要自己输出,这点完全不确认,所以注定要WA了,三是没看output后面的Note,这点本来不应该的。总来说,英语的理解能力还待提高,耐心还待培养,思维能力还待提高,最重要的还是书要看、题要做!这题涨了点经验了。
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)