JosepRivaille

X13387: Simulant el joc de la vida

Jun 5th, 2015
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. typedef vector<bool> Row;
  7. typedef vector<Row> Matrix;
  8.  
  9.  
  10. struct Punt {
  11.     int y;
  12.     int x;
  13. };
  14.  
  15. void write_matrix(const Matrix& M);
  16.  
  17. int number_of_B(const Matrix& M, int f, int c, int n) {
  18.     Punt pa, pb, pc;
  19.     pa.y = f;
  20.     pa.x = c;
  21.     pb = pc = pa;
  22.     if (pa.y > 0) {
  23.         --pa.y;
  24.         pb.y = pa.y;
  25.     }
  26.     if (pa.x > 0) --pa.x;
  27.     if (pb.x < n - 1) {
  28.         ++pb.x;
  29.         pc.x = pb.x;
  30.     }
  31.     if (pc.y < n - 1) ++pc.y;
  32.     int found = 0;
  33.     for (int i = pa.y; i <= pc.y; ++i) {
  34.             for (int j = pa.x; j <= pb.x; ++j) {
  35.                 if (M[i][j]) {
  36.                     ++found;
  37.                 }
  38.             }
  39.         }
  40.     if (M[f][c]) return found - 1;
  41.     else return found;
  42. }
  43.  
  44.  
  45. void new_matrix(Matrix& M, int n) {
  46.     bool change = false;
  47.     for (int i = 0; i < n; ++i) {
  48.         if (!change) {
  49.             for (int j = 0; j < n; ++j) {
  50.                 if (!M[j][i] && number_of_B(M, j, i, n) == 3) M[j][i] = true;
  51.                 else if (M[j][i] && number_of_B(M, j, i, n) >= 2) M[j][i] = true;
  52.                 else M[j][i] = false;
  53.             }
  54.             change = true;
  55.         }
  56.         else {
  57.             for (int j = n - 1; j >= 0; --j) {
  58.                 if (!M[j][i] && number_of_B(M, j, i, n) == 3) M[j][i] = true;
  59.                 else if (M[j][i] && number_of_B(M, j, i, n) >= 2) M[j][i] = true;
  60.                 else M[j][i] = false;
  61.  
  62.             }
  63.             change = false;
  64.         }
  65.     }
  66.     write_matrix(M);
  67. }
  68.  
  69. bool igual(const Matrix& M1, const Matrix& M2) {
  70.     int tam_m1 = M1.size();
  71.     int tam_m2 = M2.size();
  72.     for (int i = 0; i < tam_m1; ++i) {
  73.         for (int j = 0; j < tam_m2; ++j) {
  74.             if (M1[i][j] != M2[i][j]) return false;
  75.         }
  76.     }
  77.     return true;
  78. }
  79.  
  80. void write_matrix(const Matrix& M) {
  81.     int tam = M.size();
  82.     for (int i = 0; i < tam; ++i) {
  83.         for (int j = 0; j < tam; ++j) {
  84.             if (M[i][j]) cout << 'B';
  85.             else cout << '.';
  86.         }
  87.         cout << endl;
  88.     }
  89. }
  90.  
  91. int main(){
  92.     int tam, v;
  93.     cin >> tam >> v;
  94.     Matrix M(tam, Row(tam, false));
  95.     int f, c;
  96.     while (cin >> f >> c) M[f][c] = true;
  97.     write_matrix(M);
  98.     cout << endl;
  99.     Matrix N(tam, Row(tam));
  100.     N = M;
  101.     new_matrix(N, tam);
  102.     cout << endl;
  103.     --v;
  104.     bool first = true;
  105.     bool space = false;
  106.     while (v > 0 && !igual(M, N)) {
  107.         if (!first) cout << endl;
  108.         M = N;
  109.         new_matrix(N, tam);
  110.         first = false;
  111.         --v;
  112.         space = true;
  113.     }
  114.     if (space) cout << endl;
  115. }
  116.  
  117. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment