Advertisement
STANAANDREY

AOCMMXX d20p1

Apr 3rd, 2021
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const volatile int GRIDSIDE(0);
  4.  
  5. class Tile {
  6.     int id;
  7.     vector<string> cells;
  8. public:
  9.     static const short TILEWH = 10;
  10.     Tile() : id(0) {
  11.         cells.resize(TILEWH);
  12.     }
  13.     int getID() const noexcept {
  14.         return id;
  15.     }
  16.     decltype(cells) getCells() const noexcept {
  17.         return cells;
  18.     }
  19.     bool read(istream &inputStr) {
  20.         string s;
  21.         getline(inputStr, s);
  22.         if (s.empty()) {
  23.             return false;
  24.         }
  25.         for (int i = 5; isdigit(s[i]); i++) {
  26.             id = id * 10 + (s[i] - '0');
  27.         }
  28.         for (auto &it : cells) {
  29.             inputStr >> it;
  30.         }
  31.         return true;
  32.     }
  33.     void rotate90Left() {
  34.         vector<string> aux(TILEWH, string(TILEWH, '?'));
  35.         for (int i = 0; i < TILEWH; i++) {
  36.             for (int j = 0; j < TILEWH; j++) {
  37.                 aux[i][j] = cells[j][TILEWH - i - 1];
  38.             }
  39.         }
  40.         cells = aux;
  41.     }
  42.     void flipLeft() {
  43.         for (int i = 0; i < TILEWH; i++) {
  44.             for (int j = 0; j < (TILEWH >> 1); j++) {
  45.                 swap(cells[i][j], cells[i][TILEWH - j - 1]);
  46.             }
  47.         }
  48.     }
  49.     bool canBelow(const Tile &other) const noexcept {
  50.         return this->cells.back() == other.getCells()[0];
  51.     }
  52.     bool canRight(const Tile &other) const noexcept {
  53.         for (int i = 0; i < TILEWH; i++) {
  54.             if (cells[i].back() != other.getCells()[i][0]) {
  55.                 return false;
  56.             }
  57.         }
  58.         return true;
  59.     }
  60.     friend istream &operator >> (istream &inputStr, Tile &tile) {
  61.         tile.read(inputStr);
  62.         return inputStr;
  63.     }
  64. };
  65. vector<Tile> tiles;
  66. vector<vector<Tile>> grid;
  67.  
  68. void mkPositions(Tile tile) {
  69.     for (int i = 0; i < 2; i++) {
  70.         for (int j = 0; j < 4; j++) {
  71.             tiles.push_back(tile);
  72.             tile.rotate90Left();
  73.         }
  74.         tile.flipLeft();
  75.     }
  76. }
  77.  
  78. void updateGridSize(int newGridSide) {
  79.     int *ptr = (int*)&GRIDSIDE;
  80.     *ptr = newGridSide;
  81. }
  82.  
  83. void search(int row, int col, set<int> &visited) {
  84.     if (row == GRIDSIDE) {
  85.         int64_t prod = 1;
  86.         prod *= 1LL * grid[0][0].getID();
  87.         prod *= 1LL * grid.back()[0].getID();
  88.         prod *= 1LL * grid[0].back().getID();
  89.         prod *= 1LL * grid.back().back().getID();
  90.         cout << "prod: " << prod << endl;
  91.         exit(EXIT_SUCCESS);
  92.     }
  93.     for (const auto tile : tiles) {
  94.         if (!visited.count(tile.getID())) {
  95.             bool notOk = (row > 0 && !grid[row - 1][col].canBelow(tile));
  96.             notOk = notOk || (col > 0 && !grid[row][col - 1].canRight(tile));
  97.             if (notOk) {
  98.                 continue;
  99.             }
  100.             grid[row][col] = tile;
  101.             visited.insert(tile.getID());
  102.             if (col == GRIDSIDE - 1) {
  103.                 search(row + 1, 0, visited);
  104.             } else {
  105.                 search(row, col + 1, visited);
  106.             }
  107.             visited.erase(tile.getID());
  108.         }
  109.     }
  110. }
  111.  
  112. int main() {
  113.     freopen("text.txt", "r", stdin);
  114.     while (true) {
  115.         Tile tile;
  116.         if (!(cin >> tile)) {
  117.             break;
  118.         }
  119.         cin.get();
  120.         cin.get();
  121.         mkPositions(tile);
  122.     }//*/
  123.     updateGridSize(round(sqrt((int)tiles.size() / 8)));
  124.     grid = vector<vector<Tile>>(GRIDSIDE, vector<Tile>(GRIDSIDE));
  125.     set<int> visited;
  126.     search(0, 0, visited);//*/
  127.     return 0;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement