Advertisement
IzhanVarsky

Untitled

Mar 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. int matches = 0;
  9. vector<map<char, vector<int>>> wordsByMap;
  10. string words[4];
  11. ifstream in("crosswords.in");
  12. ofstream out("crosswords.out");
  13.  
  14. void checkCase(int a, int b, int c, int d) {
  15.     string top = words[a];
  16.     string bot = words[b];
  17.     string left = words[c];
  18.     string right = words[d];
  19.     map<char, vector<int>> mapTop = wordsByMap[a];
  20.     map<char, vector<int>> mapBot = wordsByMap[b];
  21.     map<char, vector<int>> mapLeft = wordsByMap[c];
  22.     map<char, vector<int>> mapRight = wordsByMap[d];
  23.     vector<int> xCoordTop;
  24.     vector<int> yCoordTop;
  25.     for (auto &it1 : mapTop) {
  26.         char topAt = it1.first;
  27.         if (mapLeft.count(topAt) != 0) {
  28.             auto it2 = mapLeft.find(topAt);
  29.             for (int i : it1.second) {
  30.                 for (int j : it2->second) {
  31.                     xCoordTop.push_back(i);
  32.                     yCoordTop.push_back(j);
  33.                 }
  34.             }
  35.         }
  36.     }
  37.     vector<int> xCoordBot;
  38.     vector<int> yCoordBot;
  39.     for (auto &it1 : mapBot) {
  40.         char botAt = it1.first;
  41.         if (mapRight.count(botAt) != 0) {
  42.             auto it2 = mapRight.find(botAt);
  43.             for (int i : it1.second) {
  44.                 for (int j : it2->second) {
  45.                     xCoordBot.push_back(i);
  46.                     yCoordBot.push_back(j);
  47.                 }
  48.             }
  49.         }
  50.     }
  51.     for (int i = 0; i < xCoordTop.size(); ++i) {
  52.         for (int j = 0; j < xCoordBot.size(); ++j) {
  53.             for (auto k = xCoordTop[i] + 1; k < top.size(); ++k) {
  54.                 for (auto t = yCoordTop[i] + 1; t < left.size(); ++t) {
  55.                     if ((xCoordBot[j] != 0 && yCoordBot[j] != 0) &&
  56.                         (((xCoordBot[j] + xCoordTop[i]) >= k)) &&
  57.                         (((yCoordBot[j] + yCoordTop[i]) >= t))) {
  58.                         if ((left[t] == bot[xCoordBot[j] + xCoordTop[i] - k]) &&
  59.                             (top[k] == right[yCoordBot[j] + yCoordTop[i] - t])) {
  60.                             matches += 2;
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69. int main() {
  70.     for (int i = 0; i < 4 ; i++) {
  71.         in >> words[i];
  72.         map<char, vector<int>> tmp;
  73.         for (int j = 0; j < words[i].length(); ++j) {
  74.             tmp[words[i][j]].push_back(j);
  75.         }
  76.         wordsByMap.push_back(tmp);
  77.     }
  78.     // top bot left right
  79.     // int olymp ifmo ramp
  80.     // 0 3 1 2
  81.     // ifmo ramp int olymp
  82.     // 1 2 0 3
  83.     checkCase(0, 1, 2, 3);
  84.     checkCase(0, 1, 3, 2);
  85.     checkCase(1, 0, 2, 3);
  86.     checkCase(1, 0, 3, 2);
  87.  
  88.     checkCase(0, 2, 1, 3);
  89.     checkCase(0, 2, 3, 1);
  90.     checkCase(2, 0, 1, 3);
  91.     checkCase(2, 0, 3, 1);
  92.  
  93.     checkCase(0, 3, 1, 2);
  94.     checkCase(0, 3, 2, 1);
  95.     checkCase(3, 0, 1, 2);
  96.     checkCase(3, 0, 2, 1);
  97.     out << matches;
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement