matheus_monteiro

Cubos Coloridos

Sep 9th, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Cube {
  5.     int upper_face, lower_face;
  6.     vector<int> middle_faces;
  7.     Cube() { middle_faces = vector<int>(4); }
  8.  
  9.     vector<int> get_cube() {
  10.         vector<int> c = middle_faces;
  11.         c.push_back(upper_face);
  12.         c.push_back(lower_face);
  13.         return c;
  14.     }
  15.  
  16.     void rotate_vert() {
  17.         int temp_upper = upper_face;
  18.         int temp_lower = lower_face;
  19.  
  20.         upper_face = middle_faces[0];
  21.         lower_face = middle_faces[2];
  22.  
  23.         middle_faces[0] = temp_lower;
  24.         middle_faces[2] = temp_upper;
  25.     }
  26.  
  27.     void rotate_horiz() {
  28.         middle_faces = vector<int> { middle_faces[1], middle_faces[2], middle_faces[3], middle_faces[0] };
  29.     }
  30. };
  31.  
  32.  
  33. vector<Cube> cubes;
  34. vector<int> father, sz;
  35. int ans;
  36.  
  37. void gen(Cube a, int k, set<vector<int>> &possibilities) {
  38.     if(k == 8) return;
  39.  
  40.     possibilities.insert(a.get_cube());
  41.  
  42.     Cube c = a, d = a;
  43.  
  44.     c.rotate_horiz();
  45.     d.rotate_vert();
  46.  
  47.     gen(c, k + 1, possibilities);
  48.     gen(d, k + 1, possibilities);
  49. }
  50.  
  51. int find(int x) {
  52.     if(x == father[x]) return x;
  53.     return father[x] = find(father[x]);
  54. }
  55.  
  56. void join(int x, int y) {
  57.     x = find(x);  y = find(y);
  58.     if(x == y) return;
  59.     if(sz[x] > sz[y]) swap(x, y);
  60.     father[x] = y;
  61.     sz[y] += sz[x];
  62.     ans--;
  63. }
  64.  
  65. int32_t main() {
  66.  
  67.     ios_base::sync_with_stdio(false);
  68.     cin.tie(nullptr);
  69.  
  70.     int n;
  71.     while(cin >> n and n) {
  72.         cubes = vector<Cube>(n);
  73.         father = vector<int>(n);
  74.         sz = vector<int>(n);
  75.         ans = n;
  76.  
  77.         for(int i= 0; i < n; i++)
  78.             father[i] = i, sz[i] = 1;
  79.        
  80.         map<vector<int>, int> existing_cube;
  81.  
  82.         for(int i = 0; i < n; i++) {
  83.             cin >> cubes[i].upper_face;
  84.             for(int j = 0; j < 4; j++)
  85.                 cin >> cubes[i].middle_faces[j];
  86.             cin >> cubes[i].lower_face;
  87.  
  88.             set<vector<int>> possibilities;
  89.  
  90.             gen(cubes[i], 0, possibilities);
  91.  
  92.             for(auto c : possibilities) {
  93.                 if(existing_cube.count(c)) {
  94.                     int j = existing_cube[c];
  95.                     join(i, j);
  96.                 }
  97.             }
  98.  
  99.             existing_cube[ cubes[i].get_cube() ] = i;
  100.         }
  101.         cout << ans << '\n';
  102.     }
  103.  
  104.     return 0;
  105. }
Add Comment
Please, Sign In to add comment