我现在要用串行的方法实现一个生命游戏,这个生命游戏是在一个100000 x 100000的棋盘上,并且繁衍500代。我选择用list形成一个二维数组来装这个棋盘上的数据,可是程序运行到一半,报错说memory error
,如果数据量小一点的话运行时没问题的。请问我应该怎么改进呢
# 棋盘的行列数
row = 100000
column = 100000
# 进化代数
count = 50
nCellSta = [[random.randint(0, 1) for i in range(column)] for j in range(row)]
nTempSta = [[0 for p in range(column)] for q in range(row)]
def CellCount(nRow, nColumn):
global row, column, nCellSta
nSum = 0
for i in range(nRow - 1, nRow + 2):
for j in range(nColumn - 1, nColumn + 2):
if i < 0 or i > row - 1 or j < 0 or j > column - 1 or i == nRow and j == nColumn:
continue
if nCellSta[i][j] == 1:
nSum += 1
if nSum == 0 or nSum == 1 or nSum == 4 or nSum == 5 or nSum == 6 or nSum == 7 or nSum == 8:
return 0
elif nSum == 2:
return nCellSta[nRow][nColumn]
elif nSum == 3:
return 1
# 输出列表nCellSta的值
def printValue():
nSum = 0
global row, column
for i in range(row):
for j in range(column):
# 打印效果
# print(nCellSta[i][j], ' ', end='')
nSum += nCellSta[i][j]
# print('\n')
return nSum
def main():
global count, nCellSta, nTempSta
printValue()
startTime = time.perf_counter()
for k in range(count - 1):
# print('新一轮游戏:\n')
for i in range(row):
for j in range(column):
nTempSta[i][j] = CellCount(i, j)
nCellSta = copy.deepcopy(nTempSta)
if not printValue():
print('全部死亡,进化结束')
break
endTime = time.perf_counter()
seconds = endTime - startTime
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print('花费时间为:%d:%02d:%.3f' % (h, m, s))
噜噜哒
慕标琳琳
相关分类