zoukankan      html  css  js  c++  java
  • Uva 457 Linear Cellular Automata

      先上题目:

    Linear Cellular Automata 

    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".

      这一题只要弄懂题意的话就是一题水题。开始题目里面某一些地方看得不是很懂,于是卡了一下,后来经人解释了一下题目的大意,然后就顺畅了。

      我的方法是先打印第一行,然后再按照题目的意思进行下面的操作。

      这里我是用了两个数组dish1和dish2。前者用来记录上一天40个培养皿的情况,后者用于储存操作后的情况,然后再将其转化为题目要求的符号输出。其中dish2[i]=dish1[i-1]+dish1[i]+dish1[i+1]。值得注的是最左边和最右边两个培养皿是dish2[1]=dish1[1]+dish1[2]以及dish2[40]=dish2[39]+dish2[40]。

    为了处理这两个特殊情况,我把dish1和dish2都开大了一点,并且初始化全为0,对数组元素的处理时1<=i<=40。

      对于换行问题,按照题目的意思来就没有错了。,只是,在每一行输出以后都要换行。

     

      上代码:

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int dna[10],i,n,m,k,dish1[45],dish2[45];
        scanf("%d",&n);
        while(n--)
        {
            memset(dish1,0,sizeof(dish1));
            memset(dish2,0,sizeof(dish2));
            memset(dna,0,sizeof(dna));
            dish1[20]=1;
            for(i=0;i<10;i++) scanf("%d",&dna[i]);
            printf("                   .                    \n");
            m=49;
            k=0;
            while(m--)
            {
                for(i=1;i<=40;i++)
                {
                    k=dish1[i-1]+dish1[i]+dish1[i+1];
                    dish2[i]=dna[k];
                    switch(dish2[i])
                    {
                        case 0:putchar(' ');break;
                        case 1:putchar('.');break;
                        case 2:putchar('x');break;
                        case 3:putchar('W');break;
                    }
                }
                printf("\n");
                for(i=1;i<=40;i++) dish1[i]=dish2[i];
            }
            if(n) printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    冲刺第九天
    冲刺第八天
    寻找小水王
    冲刺第七天
    冲刺第六天
    冲刺第五天
    寻找水王程序二
    学习进度条(第十二周)
    寻找水王程序一
    学习进度条(第十一周)
  • 原文地址:https://www.cnblogs.com/sineatos/p/2908165.html
Copyright © 2011-2022 走看看