(Python)
导入副本
def-combine(matrix):#接受矩阵矩阵并将其融合
合计=[]
对于矩阵中的行:
对于范围(0,len(矩阵[0][0])中的j:
agg_row=[]
对于行中的块:
agg_row+=块[j]
聚合.append(agg_row)
返回骨料
定义下降(seed,zero,source,bound):#通用分形构造函数
对于范围(0,bound)中的i:
对于范围内的q(0,len(源)):
对于范围内的r(0,len(源[q])):
如果源[q][r]==1:
source[q][r]=拷贝.depcopy(种子)
如果源[q][r]==0:
source[q][r]=copy.depcopy(零)
source=联合收割机(source)#熔断
返回源
定义计数(矩阵,绑定):
计数器=0
center_x,center_y=len(矩阵)//2,len(阵)//2
shift_limit=绑定//2
如果len(矩阵)%2==1,绑定%2==1:
对于范围内的i(-shift_limit,shift_limp+1):
对于范围内的j(-shift_limit,shift_limp+1):
如果矩阵[center_x+i][centery+j]==1:
计数器+=1
如果len(矩阵)%2==1,绑定%2==0:
对于范围内的i(-shift_limit,shift_liimit):
对于范围内的j(-shift_limit,shift_liimit):
如果矩阵[center_x+i][centery+j]==1:
计数器+=1
返回计数器
种子=[1,0,1],[0,1,0],[1,0,1]]
源=[[1]
zero=[[0,0,0],[0,0,0],[0,0,0]
#n=5的示例
n=5
print(count(下降(seed,zero,source,2),5))#这将构造一个3^2X3^2矩阵,并对中心5X5矩阵进行计数