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;
- //modifica las celdas de la matriz segun la reglas del ejercicio
- char consulta_celda(const matriu& m,int x, int y) {
- int cont=0;
- if(m[x+1][y] == 'B') ++cont;
- if(m[x][y+1] == 'B') ++cont;
- if(m[x+1][y+1] == 'B') ++cont;
- if(m[x-1][y] == 'B') ++cont;
- if(m[x][y-1] == 'B') ++cont;
- if(m[x-1][y-1] == 'B') ++cont;
- if(m[x-1][y+1] == 'B') ++cont;
- if(m[x-1][y-1] == 'B') ++cont;
- //una vez echas las comparaciones, comprobamos que hay que poner
- if(m[x][y] =='B') { //en el caso de que tratemos una B necessitamos que tenga 2 'B' adyacentes
- if(cont >= 2) return 'B';
- else return '.';
- }else { //en el caso de que tratemos un . necessitamos que tenga 3 'B' adyacentes
- if(cont >= 3) return 'B';
- else return '.';
- }
- }
- int main() {
- int x;
- int y;
- while(cin >> x >> y) {
- matriu m(x+2,fila(y+2)); // Creamos una matriz con espacios a la izquierda, derecha, arriba i abajo
- // introducimos la matriz, dejando el espacio a la izquierda, derecha, arriba i abajo
- for(int i=1 ; i <= x ; ++i) {
- for(int j= 1; j <= y ; ++j) {
- cin >> m[i][j];
- }
- }
- // creamos y rellenamos la matriz resultado, usando de fuente la primera y comparando con los datos que nos dan
- matriu m_res(x,fila(y));
- for(int i=0 ; i < x ; ++i) {
- for(int j=0; j < y ; ++j) {
- m_res[i][j] = consulta_celda(m,i+1,j+1);
- }
- }
- // mostramos la matriz resultado
- for(int i=0 ; i < x ; ++i) {
- for(int j= 0; j < y ; ++j) {
- cout << m_res[i][j];
- }
- cout << endl;
- }
- cout << endl;
- }
- }
Add Comment
Please, Sign In to add comment