Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef vector<char> Fila;
  6. typedef vector<Fila> Matriu;
  7.  
  8. void escriu(const Matriu& M) {
  9.     int n = M.size();
  10.     for (int i = 0; i < n; ++i) {
  11.         for (int j = 0; j < n; ++j) cout << M[i][j];
  12.         cout << endl;
  13.     }
  14.     cout << endl;
  15. }
  16.  
  17. //Per mirar els basteris adjacents, fem una funcio que ens indiqui  
  18. //si la posicio es troba fora de la matriu.
  19. bool fora_matriu(const Matriu& M, int f, int c) {
  20.     int n = M.size();
  21.     return (f < 0 or f >= n or c < 0 or c >= n);
  22. }
  23.  
  24. int bacteris_adjacents (const Matriu& M, int f, int c) {
  25.     int cont_bacteris = 0;
  26.     for (int i = f-1 ; i <= f+1; ++i) {
  27.         for (int j = c-1; j <= c+1; ++j) {
  28.             if (not fora_matriu(M,i,j)) {
  29.                 if (M[i][j] == 'B') ++cont_bacteris;
  30.             }
  31.         }
  32.     }
  33.     if (M[f][c] == 'B') --cont_bacteris;
  34.     return cont_bacteris;
  35. }
  36.  
  37. void desenvolupa_joc_vida(Matriu& M, bool& canvi, int& i, int& j) {
  38.     if (M[i][j] == '.' and bacteris_adjacents(M,i,j) == 3) {
  39.         M[i][j]= 'B';
  40.         canvi = true;
  41.     }
  42.     else if (M[i][j] == 'B' and bacteris_adjacents(M,i,j) < 2) {
  43.         M[i][j]= '.';
  44.         canvi = true;
  45.     }
  46. }
  47.                          
  48. void fer_volta(Matriu& M, int v, bool& canvi) {
  49.     if (canvi and v != 0) {
  50.         int n = M.size();
  51.         canvi = false;
  52.         for (int j = 0; j < n; ++j) {
  53.             if (j%2 == 0) {
  54.                 for (int i = 0; i < n; ++i) {
  55.                     desenvolupa_joc_vida(M,canvi,i,j);
  56.                 }
  57.             }
  58.             else {
  59.                 for (int k = n-1; k >= 0; --k) {
  60.                     desenvolupa_joc_vida(M,canvi,k,j);
  61.                 }
  62.             }
  63.         }
  64.         escriu(M);
  65.         fer_volta(M,v-1,canvi);
  66.     }
  67. }
  68.  
  69. int main() {
  70.     int n;
  71.     cin >> n;
  72.     Matriu M(n, vector<char>(n,'.'));
  73.     int v;
  74.     cin >> v;
  75.     int f, c;
  76.     while (cin >> f >> c) M[f][c] = 'B';
  77.     escriu(M);
  78.     bool canvi = true;
  79.     fer_volta(M,v,canvi);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement