uva Linear Cellular Automata

本文介绍了一种模拟细菌殖民地生长的程序设计方法。通过给定的DNA信息数组,模拟了40个培养皿中细菌密度的变化过程,并规定了特定的初始条件。详细解释了如何根据相邻培养皿的密度来确定当前培养皿的未来密度。

题目如下:

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


 题目若读懂了就是简单题,刚开始把sum[i][j]算出来后没立即对应到dna中,而是算完之后再一一对应,导致产生的sum[i][j]是随机数,dna数组越界,费了不少时间,改正这错误后成功AC。

AC的代码如下:

#include
#include
using namespace std;
int dna[10],n,i,j,k;
int c[42],sum[50][42];
int main()
{
cin>>n;
while(n--)
{for(i=0;i<=9;i++)
cin>>dna[i];
for(i=0;i<=41;i++)
if(i==20)
c[i]=1;
else 
c[i]=0;
for(i=0;i<=41;i++)
sum[0][i]=c[i];
for(i=0;i<=49;i++)
{sum[i][0]=0;
sum[i][41]=0;}
for(i=1;i<=49;i++)
for(j=1;j<=40;j++)
{sum[i][j]=sum[i-1][j-1]+sum[i-1][j+1]+sum[i-1][j];
k=sum[i][j];
sum[i][j]=dna[k];}
for(i=0;i<=49;i++)
{for(j=1;j<=40;j++)
{if(sum[i][j]==0)
cout<<" ";
if(sum[i][j]==1)
cout<<".";
if(sum[i][j]==2)
cout<<"x";
if(sum[i][j]==3)
cout<<"W";}
cout<0)
cout<

 

 

<think>我们正在讨论的是Unity游戏开发中的地形生成技术。Cellular Automata(细胞自动机)是一种常用于程序化生成的技术,特别适合生成自然形态的结构,如洞穴、地形等。在之前的上下文中,用户询问了2D地下洞穴沙盒游戏的准备事项,其中提到了Cellular Automata用于洞穴优化。现在用户直接询问Cellular Automata是什么,因此需要给出一个清晰的定义,并说明它在游戏开发中的应用,特别是洞穴生成方面。 回答结构: 1. 定义Cellular Automata(细胞自动机)的基本概念。 2. 解释它在游戏开发中的应用场景(尤其是洞穴生成)。 3. 给出一个简单的算法步骤(伪代码或代码示例)来说明如何用细胞自动机生成洞穴。 4. 说明其优缺点。 5. 最后按照要求提出3-5个相关问题。 注意:用户要求使用中文回答,并在最后提出相关问题,且第一行必须是“§§相关问题§§”,然后列出问题。 由于之前用户的问题涉及Unity游戏开发,因此回答应侧重在游戏开发中的应用。</think>### Cellular Automata(细胞自动机)详解 Cellular Automata(细胞自动机)是一种**离散计算模型**,通过简单规则和局部交互模拟复杂系统行为。在游戏开发(尤其是洞穴生成)中,它通过网格细胞的生死演化创造有机地形结构。 #### 核心原理 1. **网格系统** - 将地图划分为二维网格(每个单元格代表岩石/空地) - 初始状态随机分配(如55%岩石,45%空地) 2. **演化规则**(经典洞穴生成算法) ```python # 简化版细胞自动机洞穴生成 def cellular_automata(grid, iterations=5): new_grid = grid.copy() for _ in range(iterations): for x in range(1, len(grid)-1): for y in range(1, len(grid[0])-1): # 统计周围8格岩石数量 rock_count = sum(grid[i][j] for i in [x-1,x,x+1] for j in [y-1,y,y+1]) - grid[x][y] # 生死规则(可调整参数) if rock_count >= 5: # 周围岩石多则变岩石 new_grid[x][y] = 1 elif rock_count <= 3: # 周围空地多则变空地 new_grid[x][y] = 0 return new_grid ``` #### 游戏开发应用场景 | 应用方向 | 实现效果 | 参数调整要点 | |----------------|------------------------------|---------------------------| | 洞穴生成 | 创建自然形态的连通洞穴 | 迭代次数(4-7次最佳) | | 矿脉分布 | 生成簇状矿石聚集区 | 初始密度(0.4-0.6) | | 地下河塑造 | 生成蜿蜒水系 | 边界条件(封闭/循环) | | 可破坏地形 | 实时模拟地形塌陷效果 | 邻居范围(4邻/8邻) | #### 优势与局限 - **✅ 优势** - 算法简单高效(复杂度O(n)) - 自然生成有机形态(无需人工设计) - 通过规则微调实现多样化地形 - **❌ 局限** - 可能产生孤立区域(需后处理连通性检测) - 过度平滑导致特征缺失(需配合噪声算法) - 参数敏感(需大量测试平衡) > **实践技巧**:配合 **Flood Fill 算法** 检测洞穴连通性,使用 **A*寻路** 验证可达性,最后用 **边缘平滑算法** 优化视觉效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值