Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef vector<char> Fila;
- typedef vector<Fila> Matriu;
- void escriu(const Matriu& M) {
- int n = M.size();
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) cout << M[i][j];
- cout << endl;
- }
- cout << endl;
- }
- //Per mirar els basteris adjacents, fem una funcio que ens indiqui
- //si la posicio es troba fora de la matriu.
- bool fora_matriu(const Matriu& M, int f, int c) {
- int n = M.size();
- return (f < 0 or f >= n or c < 0 or c >= n);
- }
- int bacteris_adjacents (const Matriu& M, int f, int c) {
- int cont_bacteris = 0;
- for (int i = f-1 ; i <= f+1; ++i) {
- for (int j = c-1; j <= c+1; ++j) {
- if (not fora_matriu(M,i,j)) {
- if (M[i][j] == 'B') ++cont_bacteris;
- }
- }
- }
- if (M[f][c] == 'B') --cont_bacteris;
- return cont_bacteris;
- }
- void desenvolupa_joc_vida(Matriu& M, bool& canvi, int& i, int& j) {
- if (M[i][j] == '.' and bacteris_adjacents(M,i,j) == 3) {
- M[i][j]= 'B';
- canvi = true;
- }
- else if (M[i][j] == 'B' and bacteris_adjacents(M,i,j) < 2) {
- M[i][j]= '.';
- canvi = true;
- }
- }
- void fer_volta(Matriu& M, int v, bool& canvi) {
- if (canvi and v != 0) {
- int n = M.size();
- canvi = false;
- for (int j = 0; j < n; ++j) {
- if (j%2 == 0) {
- for (int i = 0; i < n; ++i) {
- desenvolupa_joc_vida(M,canvi,i,j);
- }
- }
- else {
- for (int k = n-1; k >= 0; --k) {
- desenvolupa_joc_vida(M,canvi,k,j);
- }
- }
- }
- escriu(M);
- fer_volta(M,v-1,canvi);
- }
- }
- int main() {
- int n;
- cin >> n;
- Matriu M(n, vector<char>(n,'.'));
- int v;
- cin >> v;
- int f, c;
- while (cin >> f >> c) M[f][c] = 'B';
- escriu(M);
- bool canvi = true;
- fer_volta(M,v,canvi);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement