-
Notifications
You must be signed in to change notification settings - Fork 5
/
P26100_ca_El_joc_de_la_vida__1_.cc
44 lines (43 loc) · 1.37 KB
/
P26100_ca_El_joc_de_la_vida__1_.cc
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
#include <iostream>
#include <vector>
using namespace std;
int main () {
size_t x,y;
bool first=true;
while (cin >> x >> y and x!=0 and y!=0) {
if (!first) cout << endl;
else first=false;
vector <vector <bool> > m (x,vector <bool> (y,0));
vector <vector <bool> > m2 = m;
for (size_t i = 0; i < x; ++i)
for (size_t j = 0; j < y; ++j) {
char b;
cin >> b;
if (b=='B') m[i][j]=true;
}
for (size_t i = 0; i < x; ++i)
for (size_t j = 0; j < y; ++j) {
int tot=0;
if (i > 0) {
tot += m[i-1][j];
if (j > 0) tot += m[i-1][j-1];
if (j < y-1) tot += m[i-1][j+1];
}
if (i < x-1) {
tot += m[i+1][j];
if (j > 0) tot += m[i+1][j-1];
if (j < y-1) tot += m[i+1][j+1];
}
if (j > 0) tot += m[i][j-1];
if (j < y-1) tot += m[i][j+1];
if (m[i][j]) m2[i][j]=(tot==2 or tot==3);
else m2[i][j]=(tot==3);
}
for (size_t i = 0; i < x; ++i) {
for (size_t j = 0; j < y; ++j)
if (m2[i][j]) cout << 'B';
else cout << '.';
cout << endl;
}
}
}