GastonFontenla

N2P1 - Plastetris (solución 2)

Sep 1st, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int dx[] = {1, 0, -1, 0};
  6. int dy[] = {0, 1, 0, -1};
  7.  
  8. bool esValido(int x, int y)
  9. {
  10.     return 0 <= x && x < 4 && 0 <= y && y < 4;
  11. }
  12.  
  13. string plastetris(vector <string> cajita)
  14. {
  15.     /**
  16.     Genero todos los tableros válidos
  17.     y luego compruebo si alguno es igual al input
  18.     **/
  19.     vector <vector <vector <string> > > tableros(4);
  20.  
  21.     vector <string> tableroVacio = {"....", "....", "....", "...."};
  22.     for(int i=0; i<4; i++)
  23.     {
  24.         for(int j=0; j<4; j++)
  25.         {
  26.             tableroVacio[i][j] = 'X';
  27.             tableros[0].push_back(tableroVacio);
  28.             tableroVacio[i][j] = '.';
  29.         }
  30.     }
  31.  
  32.     for(int i=1; i<tableros.size(); i++)
  33.     {
  34.         for(int j=0; j<tableros[i-1].size(); j++)
  35.         {
  36.             /**
  37.             Por cada tablero, pruebo convertir
  38.             un '.' en una 'X'
  39.             solo si tiene una vecina que ya es X
  40.             **/
  41.  
  42.             auto tablero = tableros[i-1][j];
  43.             for(int x=0; x<4; x++)
  44.             {
  45.                 for(int y=0; y<4; y++)
  46.                 {
  47.                     if(tablero[x][y] == '.')
  48.                     {
  49.                         bool valido = false;
  50.                         for(int k=0; k<4; k++)
  51.                         {
  52.                             int x2 = x+dx[k];
  53.                             int y2 = y+dy[k];
  54.                             if(esValido(x2, y2) && tablero[x2][y2] == 'X')
  55.                             {
  56.                                 valido = true;
  57.                                 break;
  58.                             }
  59.                         }
  60.                         if(valido == true)
  61.                         {
  62.                             tablero[x][y] = 'X';
  63.                             tableros[i].push_back(tablero);
  64.                             tablero[x][y] = '.';
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     for(int i=0; i<tableros[3].size(); i++)
  73.         if(tableros[3][i] == cajita)
  74.             return "SI";
  75.  
  76.     return "NO";
  77. }
  78.  
  79. /**
  80. //Función main auxiliar para testear
  81. int main()
  82. {
  83.     vector <string> cajita(4);
  84.     for(int i=0; i<4; i++)
  85.         cin >> cajita[i];
  86.  
  87.     cout << plastetris(cajita) << endl;
  88.  
  89.     return 0;
  90. }
  91. **/
Advertisement
Add Comment
Please, Sign In to add comment