2013
11-26

# A New Growth Industry

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 (20×20). 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 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.

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.

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

##!.................
#!..................
!...................
....................
....................
....................
....................
.........!..........
........!#!.........
.......!#X#!........
........!#!.........
.........!..........
....................
....................
....................
....................
....................
....................
....................
....................

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int mat[20][20],tar[20][20],tar2[20][20];
char transtar[20][20];
int pxvalue[16];

int main()
{
int testcase;
cin>>testcase;
for(int s=1;s<=testcase;s++)
{
memset(mat,0,sizeof(mat));
memset(tar,0,sizeof(tar));
memset(pxvalue,0,sizeof(pxvalue));
int day,tmp,tmpx,tmpy;
cin>>day;

for(int i=0;i<16;i++)
{
cin>>pxvalue[i];
}

for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
cin>>mat[i][j];
}
}

for(int k=0;k<day;k++)
{
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
tmp=mat[i][j];
for(int z=0;z<4;z++)
{
tmpx=i+dir[z][0];
tmpy=j+dir[z][1];
if(tmpx>=0 && tmpx<20 && tmpy>=0 && tmpy<20)
tmp+=mat[tmpx][tmpy];
}

tar[i][j] = mat[i][j]+pxvalue[tmp];

if(tar[i][j]>3)
tar[i][j]=3;
if(tar[i][j]<0)
tar[i][j]=0;
}
}
memcpy(mat,tar,sizeof(mat));    //滚动更新，重中之重
}

for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
if(tar[i][j]==0)
{
transtar[i][j]='.';
}
else if(tar[i][j]==1)
{
transtar[i][j]='!';
}
else if(tar[i][j]==2)
{
transtar[i][j]='X';
}
else if(tar[i][j]==3)
transtar[i][j]='#';
}
}

for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
cout<<transtar[i][j];
}
cout<<endl;
}
if(s!=testcase)
cout<<endl;

}
return 0;
}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
O(n) = O(n-1) + O(n-2) + ….
O(n-1) = O(n-2) + O(n-3)+ …
O(n) – O(n-1) = O(n-1)
O(n) = 2O(n-1)