SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <vector> | |
3 | ||
4 | using namespace std; | |
5 | ||
6 | void dibuja(const vector<vector<int> >& v) { | |
7 | int n = v.size(); | |
8 | int m = v[0].size(); | |
9 | ||
10 | for (int i = 0; i < n; ++i) { | |
11 | for (int j = 0; j < m; ++j) { | |
12 | cout << v[i][j]; | |
13 | } | |
14 | cout << endl; | |
15 | } | |
16 | } | |
17 | ||
18 | bool dentroMatriz(int y, int x, int n, int m) { | |
19 | if (y < 0 || y >= n) | |
20 | return false; | |
21 | ||
22 | if (x < 0 || x >= m) | |
23 | return false; | |
24 | ||
25 | return true; | |
26 | } | |
27 | ||
28 | vector<vector<int> > cuentaVecinos(const vector<vector<int> >& v) { | |
29 | int n = v.size(); | |
30 | int m = v[0].size(); | |
31 | ||
32 | vector<vector<int> > vecinos(n, vector<int>(m)); | |
33 | ||
34 | for (int i = 0; i < n; ++i) { | |
35 | for (int j = 0; j < m; ++j) { | |
36 | int suma = 0; | |
37 | ||
38 | if (dentroMatriz(i-1, j-1, n, m)) { | |
39 | suma += v[i-1][j-1]; | |
40 | } | |
41 | ||
42 | if (dentroMatriz(i-1, j, n, m)) { | |
43 | suma += v[i-1][j]; | |
44 | } | |
45 | ||
46 | if (dentroMatriz(i-1, j+1, n, m)) { | |
47 | suma += v[i-1][j+1]; | |
48 | } | |
49 | ||
50 | if (dentroMatriz(i, j-1, n, m)) { | |
51 | suma += v[i][j-1]; | |
52 | } | |
53 | ||
54 | if (dentroMatriz(i, j+1, n, m)) { | |
55 | suma += v[i][j+1]; | |
56 | } | |
57 | ||
58 | if (dentroMatriz(i+1, j-1, n, m)) { | |
59 | suma += v[i+1][j-1]; | |
60 | } | |
61 | ||
62 | if (dentroMatriz(i+1, j, n, m)) { | |
63 | suma += v[i+1][j]; | |
64 | } | |
65 | ||
66 | if (dentroMatriz(i+1, j+1, n, m)) { | |
67 | suma += v[i+1][j+1]; | |
68 | } | |
69 | ||
70 | vecinos[i][j] = suma; | |
71 | } | |
72 | } | |
73 | ||
74 | return vecinos; | |
75 | } | |
76 | ||
77 | void proximaGeneracion(vector<vector<int> >& T) { | |
78 | vector<vector<int> > V = cuentaVecinos(T); | |
79 | ||
80 | int n = T.size(); | |
81 | int m = T[0].size(); | |
82 | ||
83 | for (int i = 0; i < n; ++i) { | |
84 | for (int j = 0; j < m; ++j) { | |
85 | ||
86 | if (T[i][j] == 0) { | |
87 | if(V[i][j] == 3) { | |
88 | T[i][j] = 1; | |
89 | } | |
90 | } | |
91 | ||
92 | else { | |
93 | if (!(V[i][j] == 2 || V[i][j] == 3)) { | |
94 | T[i][j] = 0; | |
95 | } | |
96 | } | |
97 | ||
98 | } | |
99 | } | |
100 | ||
101 | } | |
102 | ||
103 | ||
104 | int main() { | |
105 | int n, m; | |
106 | cout << "Introdzca las dimensiones del juego:" << endl; | |
107 | cin >> n >> m; | |
108 | ||
109 | vector<vector<int> > T(n, vector<int>(m, 0)); | |
110 | ||
111 | int insectos; | |
112 | cout << "Introduzca el numero de insectos:" << endl; | |
113 | cin >> insectos; | |
114 | ||
115 | cout << "Introduzca las coordenadas de los insectos:" << endl; | |
116 | for (int i = 0; i < insectos; ++i) { | |
117 | int x, y; | |
118 | - | cin >> y >> x; |
118 | + | cin >> x >> y; |
119 | ||
120 | if (dentroMatriz(y-1, x-1, n, m)) { | |
121 | T[y-1][x-1] = 1; | |
122 | } | |
123 | } | |
124 | ||
125 | dibuja(T); | |
126 | ||
127 | int ciclos; | |
128 | cout << endl << "Introduzca el numero de ciclos:" << endl; | |
129 | cin >> ciclos; | |
130 | ||
131 | for (int i = 0; i < ciclos; ++i) { | |
132 | proximaGeneracion(T); | |
133 | } | |
134 | ||
135 | dibuja(T); | |
136 | } |