1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <cmath>
#include <vector>
#include <array>
#include <cstdlib>
using namespace std;
const int N = 501;
class Cell{
int state;
int burning;
public:
void sets(int s){
state = s;
}
int gets(){
return state;
}
void setburning(int b){
burning = b;
}
int getburning(){
return burning;
}
Cell(){
state = 0;
burning = 0;
}
Cell(int s, int b){
state = s;
burning = b;
}
Cell operator= (Cell C){
int s = C.gets();
int b = C.getburning();
return Cell(s,b);
}
};
typedef array<array<Cell,N+2>,N+2> Cells;
int BurningNeighbors(Cells CA, int i, int j){
int Burn;
Burn = CA[i+1][j-1].getburning()+CA[i+1][j].getburning()+CA[i+1][j+1].getburning()+
CA[i][j-1].getburning()+CA[i][j+1].getburning()+CA[i-1][j-1].getburning()
+CA[i-1][j].getburning()+CA[i-1][j+1].getburning();
return Burn;
}
Cells evol(Cells CA, double p){
Cells CA2 = CA;
for (int i = 1; i < N+1; i++){
for (int j = 1; j < N+1; j++){
if (CA[i][j].gets() == 1){
CA2[i][j].sets(2);
}
else if (CA[i][j].gets() == 2){
CA2[i][j].sets(3);
CA2[i][j].setburning(0);
}
else if (CA[i][j].gets() == 0) {
int Burn = BurningNeighbors(CA,i,j);
if (Burn > 0){
double Guess = (double) (rand() % 1000)/1000.;
if (Guess < 1-pow(1-p,Burn)) {
//cout << "Guess: " << Guess << "; 1-(1-p)^Burn: " << 1-pow(1-p,Burn) << endl;
CA2[i][j].sets(1);
CA2[i][j].setburning(1);
}
}
}
}
}
return CA2;
}
int isBurning(Cells CA){
for (int i = 1; i < N+1; i++){
for (int j = 1; j < N+1; j++){
if (CA[i][j].getburning() == 1){
return 1;
}
}
}
return 0;
}
int main()
{
// Creation of Cells vector
cerr << "Beginning " << endl;
double P = 0.2;
vector<Cells> *CAs = new vector<Cells>();
// Initialisation
cout << endl << "Initialisation" << endl << endl;
static Cells CA;
for (int j = 0; j < N+2; j++){
CA[0][j].sets(3);
CA[N+1][j].sets(3);
}
for (int i = 0; i < N+2; i++){
CA[i][0].sets(3);
CA[i][N+1].sets(3);
}
CA[floor(N/2)+1][floor(N/2)+1].sets(1);
CA[floor(N/2)+1][floor(N/2)+1].setburning(1);
CAs->push_back(CA);
int T = 0;
while (isBurning(CAs->at(CAs->size()-1))){
T++;
cout << endl << "time step:" << T << endl << endl;
CAs->push_back(evol(CAs->at(CAs->size()-1),P));
}
return 0;
} |
Partager