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#!........ ........!#!......... .........!.......... .................... .................... .................... .................... .................... .................... .................... ....................
Source: Mid-Atlantic USA 2001
还真是难看懂啊
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
int n,day;
int dna[16];
char den[4]={'.','!','X','#'};
cin>>n;
while(n--)
{
cin>>day;
int a[22][22]={0};
int b[22][22]={0};
for(int i=0;i<16;i++)
cin>>dna[i];
for(int i=1;i<21;i++)
for(int j=1;j<21;j++)
cin>>a[i][j];
while(day--)
{
for(int i=1;i<21;i++)
for(int j=1;j<21;j++)
{
int k=0;
k=a[i][j]+a[i+1][j]+a[i-1][j]+a[i][j+1]+a[i][j-1];
b[i][j]=a[i][j]+dna[k];
if(b[i][j]>3)
b[i][j]=3;
if(b[i][j]<0)
b[i][j]=0;
}
memcpy(a,b,sizeof(b));
}
for(int i=1;i<21;i++)
{
for(int j=1;j<21;j++)
cout<<den[a[i][j]];
cout<<endl;
}
if(n)
cout<<endl;
}
return 0;
}
介绍一下 memcpy的用法:
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include
功能:由src所指内存区域复制count个字节到dest所指内存区域。
说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。
举例:
// memcpy.c
#include
#include
main()
{
char *s="Golden Global View";
char d[20];
clrscr();
memcpy(d,s,strlen(s));
d[strlen(s)]=0;
printf("%s",d);
getchar();
return 0;
}
相关函数:memccpy,memmove,strcpy
关于strcpy()与memcpy()比较:
在使用这两个函数中,平时大多都使用strcpy()而忽略了 memcpy(),
它们都是从缓冲区拷贝内容。
byte a[4];// 每一个字节赋数值类型
a[0]=0;
a[1]=1;
a[2]=0;
a[3]=1;
byte c1[4];
memcpy(c1, a, sizeof(byte)*4);
byte c2[4];
strcpy((char*)(byte*)c2, (char*)(byte*)a);
只有memcpy()处理的是正确的,所以拿来它们的函数原型比较一下:
char *strcpy( char *strDest, const char *strSource);
void *memcpy( void *dest, const void *src, size_t count );
可以看出 strncpy() 是处理的字符串(遇零结束),
memcpy() 是处理一个缓冲区(void*类型的),
而我们的内容中有数字0,
而数字0又是字符串的结尾字符 ' \0' 的数字表现,
字符串拷贝是遇到0就结束,
所以,如果要拷贝的缓冲区如果是非字符串那么就尽量用memcpy(),
这样可以避免出错的可能。