Guest User

Untitled

a guest
Apr 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 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. //modifica las celdas de la matriz segun la reglas del ejercicio
  9. char consulta_celda(const matriu& m,int x, int y) {
  10.     int cont=0;
  11.     if(m[x+1][y] == 'B')    ++cont;
  12.     if(m[x][y+1] == 'B')    ++cont;
  13.     if(m[x+1][y+1] == 'B')  ++cont;
  14.     if(m[x-1][y] == 'B')    ++cont;
  15.     if(m[x][y-1] == 'B')    ++cont;
  16.     if(m[x-1][y-1] == 'B')  ++cont;
  17.     if(m[x-1][y+1] == 'B')  ++cont;
  18.     if(m[x-1][y-1] == 'B')  ++cont;
  19.     //una vez echas las comparaciones, comprobamos que hay que poner
  20.     if(m[x][y] =='B') { //en el caso de que tratemos una B necessitamos que tenga 2 'B' adyacentes
  21.         if(cont >= 2) return 'B';
  22.         else return '.';
  23.     }else { //en el caso de que tratemos un . necessitamos que tenga 3 'B' adyacentes
  24.     if(cont >= 3) return 'B';
  25.         else return '.';
  26.     }
  27. }
  28.  
  29. int main() {
  30.     int x;
  31.     int y; 
  32.     while(cin >> x >> y) {
  33.         matriu m(x+2,fila(y+2)); // Creamos una matriz con espacios a la izquierda, derecha, arriba i abajo
  34.         // introducimos la matriz, dejando el espacio a la izquierda, derecha, arriba i abajo
  35.         for(int i=1 ; i <= x ; ++i) {
  36.             for(int j= 1; j <= y ; ++j) {
  37.                 cin >> m[i][j];
  38.             }
  39.         }
  40.         // creamos y rellenamos la matriz resultado, usando de fuente la primera y comparando con los datos que nos dan
  41.         matriu m_res(x,fila(y));
  42.         for(int i=0 ; i < x ; ++i) {
  43.             for(int j=0; j < y ; ++j) {
  44.                 m_res[i][j] = consulta_celda(m,i+1,j+1);
  45.             }
  46.         }
  47.         // mostramos la matriz resultado
  48.         for(int i=0 ; i < x ; ++i) {
  49.             for(int j= 0; j < y ; ++j) {
  50.                 cout << m_res[i][j];
  51.             }
  52.             cout << endl;
  53.         }
  54.         cout << endl;
  55.     }
  56. }
Add Comment
Please, Sign In to add comment