Advertisement
Tarche

Teselas-Tarche

Dec 2nd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
  6. int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
  7.  
  8. void intercambiar(int fila1, int columna1, int fila2, int columna2);
  9.  
  10. struct llamada {
  11.     int a, b, c, d;
  12. };
  13.  
  14. int teselas(vector<string> &mosaico) {
  15.     int n = (int)mosaico.size();
  16.     int m = (int)mosaico[0].size();
  17.     vector<int> amount(26, 0);
  18.    
  19.     for (int i = 0; i < n; i++) {
  20.         for (int j = 0; j < m; j++) {
  21.             amount[mosaico[i][j] - 'a']++;
  22.         }
  23.     }
  24.  
  25.     if (n % 2 == 0) n--;
  26.     if (m % 2 == 0) m--;
  27.  
  28.     int maxAmount = ((n - 1) / 2 + 1) * ((m - 1) / 2 + 1);
  29.     for (int i = 0; i < 26; i++) {
  30.         if (amount[i] > maxAmount) return 0;
  31.     }
  32.  
  33.     n = (int)mosaico.size();
  34.     m = (int)mosaico[0].size();
  35.  
  36.     vector<string> nuevo(n, string(m, ' '));
  37.  
  38.     priority_queue<pair<int, char>> letras;
  39.     for (int a = 0; a < 26; a++)
  40.         if (amount[a] != 0)
  41.             letras.push({amount[a], 'a' + a});
  42.    
  43.     while (!letras.empty()) {
  44.         auto curr = letras.top();
  45.         letras.pop();
  46.         bool done = false;
  47.         for (int i = 0; i < n; i++) {
  48.             for (int j = 0; j < m; j++) {
  49.                 if (nuevo[i][j] != ' ') continue;
  50.                 bool possible = true;
  51.                 for (int k = 0; k < 8; k++) {
  52.                     int x = i + dx[k];
  53.                     int y = j + dy[k];
  54.                     if (x < 0 || y < 0 || x >= n || y >= m) continue;
  55.                     if (nuevo[x][y] == curr.second) possible = false;
  56.                 }
  57.                 if (possible) {
  58.                     nuevo[i][j] = curr.second;
  59.                     done = true;
  60.                     break;
  61.                 }
  62.             }
  63.             if (done) break;
  64.         }
  65.         if (curr.first > 1) letras.push({curr.first - 1, curr.second});
  66.     }
  67.  
  68.     vector<vector<bool>> cambio(n, vector<bool>(m, false));
  69.     for (int i = 0; i < n; i++) {
  70.         for (int j = 0; j < m; j++) {
  71.             if (mosaico[i][j] != nuevo[i][j]) cambio[i][j] = true;
  72.         }
  73.     }
  74.  
  75.     queue<llamada> ans;
  76.     for (int i = 0; i < n; i++) {
  77.         for (int j = 0; j < m; j++) {
  78.             if (!cambio[i][j]) continue;
  79.             for (int a = 0; a < n; a++) {
  80.                 bool cambiada = false;
  81.                 for (int b = 0; b < m; b++) {
  82.                     if (!cambio[a][b]) continue;
  83.                     if (mosaico[a][b] == nuevo[i][j]) {
  84.                         ans.push({i, j, a, b});
  85.                         cambio[i][j] = false;
  86.                         mosaico[a][b] = mosaico[i][j];
  87.                         mosaico[i][j] = nuevo[i][j];
  88.                         if (mosaico[a][b] == nuevo[a][b]) cambio[a][b] = false;
  89.                         cambiada = true;
  90.                         break;
  91.                     }
  92.                 }
  93.                 if (cambiada) break;
  94.             }
  95.         }
  96.     }
  97.  
  98.     for (int i = 0; i < n; i++) {
  99.         for (int j = 0; j < m; j++) {
  100.             if (cambio[i][j])
  101.                 return 0;
  102.         }
  103.     }
  104.  
  105.     while (!ans.empty()) {
  106.         auto curr = ans.front();
  107.         ans.pop();
  108.         intercambiar(curr.a, curr.b, curr.c, curr.d);
  109.     }
  110.  
  111.     return 1;
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement