博客
关于我
04.沙堆模型
阅读量:461 次
发布时间:2019-03-06

本文共 1293 字,大约阅读时间需要 4 分钟。

"""沙堆模型:1. 在一个二维正方格子上,每个格点上都可以放上一定的沙子;2. 当某个格子 (i,j) 里的沙子数目大于 4 时,就会发生崩塌(或者也可以设定为按照一定的概率崩塌);3. 格子 (i,j) 里面的 4 颗沙子则转移到 (i,j-1),(i,j+1),(i-1,j),(i+1,j) 四个格子中;4. 随着这四个格子中沙粒数目的变化,上述的演化规则可以进一步逐层扩展到其它相邻的格点,最终蔓延整个沙堆。"""import numpy as npimport matplotlib.pyplot as plt  def iterate(grid):    changed = False    for ii, arr in enumerate(grid):  # ii:矩阵行号, jj:矩阵列号, 便于定位        for jj, val in enumerate(arr):  # 沙子数目大于4发生崩塌            if val >= 4:                grid[ii, jj] -= 4                # 满足条件的前提下移动沙子                if ii >= 1:                    grid[ii - 1, jj] += 1                if ii <= len(arr) - 1:                    grid[ii + 1, jj] += 1                if jj >= 1:                    grid[ii, jj - 1] += 1                if jj <= len(arr) - 1:                    grid[ii, jj + 1] += 1                # 矩阵是否还在变化的标志                changed = True    return grid, changed def simulate(grid):    # 循环递归,直到矩阵不再变化为止    while True:        grid, changed = iterate(grid)        if not changed:            return grid  if __name__ == "__main__":    start_grid = np.zeros((10, 10))    start_grid[3:4, 6:7] = 64    final_grid = simulate(start_grid.copy())    plt.figure()    plt.gray()    plt.imshow(start_grid)    plt.show()    plt.figure()    plt.gray()    plt.imshow(final_grid)    plt.show()

 

转载地址:http://rekbz.baihongyu.com/

你可能感兴趣的文章
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>