1. 程式人生 > >傳播模型——簡單的元胞自動機(3)

傳播模型——簡單的元胞自動機(3)

參考書目《元胞自動機理論研究及其模擬應用》科學出版社
鄰居型別:Moore鄰居
元胞狀態:0  1 (正方與反方)
演化規則:當週圍元胞狀態為1的個數多餘4,則元胞狀態變為1,否則為0 (992號規則)
演化特點:每個元胞傾向於其周圍佔大多數的意見,若干步之後基本達穩定,且相同狀態的元胞傾向於聚在一起,呈現出“物以類聚”的現象
與鄰居接觸較廣的規則聚類現象明顯於接觸較少的規則。反應出在傳播模型中人際交往密切的網路其輿論引導性更強
 

#include<Windows.h>
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>

int main()
{
	int orgData[100][100], resData[100][100];
	int nCount, nRows, nCols, i, j, times;
	int GraphDriver = DETECT, GraphMode;
	int rnd;
	//	srand(time(0));
	for (i = 0; i < 100; i++)
		for (j = 0; j < 100; j++)
		{
			rnd = rand() % 100;
			if (rnd < 50)
				orgData[i][j] = 0;
			else orgData[i][j] = 1;
		}
	initgraph(&GraphDriver, &GraphMode, "");
	setcolor(WHITE);
	rectangle(270, 190, 370, 290);
	for (nRows = 0; nRows < 100; nRows++)
		for (nCols = 0; nCols < 100; nCols++)
		{
			int tx, ty, rx, ry;
			nCount = 0;
			for (tx = nRows - 1; tx <= nRows + 1; tx++)
				for (ty = nCols - 1; ty <= nCols + 1; ty++)
				{
					if (tx >= 0)
						if (tx < 100)
							rx = tx;
						else rx = tx - 100;
					else rx = tx + 100;
					if (ty >= 0)
						if (ty < 100)
							ry = ty;
						else ry = ty - 100;
					else ry = ty + 100;
					if (orgData[rx][ry] == 1)
						nCount++;
				}
			if (nCount >= 5)
			{
				putpixel(nCols + 270, 190 + nRows, BLACK);
				resData[nRows][nCols] = 1;
			}
			else
			{
				resData[nRows][nCols] = 0;
				putpixel(nCols + 270, 190 + nRows, WHITE);
			}
			for (i = 1; i < 99; i++)
				for (j = 1; j < 99; j++)
					orgData[i][j] = resData[i][j];
			
		}
	system("pause");
	return 0;
}