Advertisement
Korotkodul

N5 6

Oct 25th, 2022
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49.  
  50. bool sh = 1;
  51.  
  52. void cvch(vector <vector <char> > a) {
  53.     for (auto v: a) {
  54.         for (auto i: v) {
  55.             cout << i;
  56.         } cout << "\n";
  57.     } cout << "\n";
  58. }
  59.  
  60.  
  61. vector <vector <bool> > was;
  62.  
  63.  
  64. int n, m, inf = 2e9;
  65.  
  66.  
  67. vector <vector <int> > mtr;
  68.  
  69. struct rct {
  70.     int x1, x2, y1, y2;
  71.     void upd(int i, int j, int n, int m) {
  72.         int yi = n - 1 - i;
  73.         int xj = j;
  74.         if (sh) {
  75.             //cout << "i j = " << i << ' ' << j << "\n";
  76.             //cout << "yi xj = " << yi << ' ' << xj << "\n";
  77.         }
  78.         x1 = min(x1, xj);
  79.         x2 = max(x2, xj);
  80.         y1 = min(y1, yi);
  81.         y2 = max(y2, yi);
  82.     }
  83.  
  84.     void see() {
  85.         cout << x1 << ' ' << x2 << ' ' << y1 << ' ' << y2 << "\n";
  86.     }
  87. };
  88.  
  89. rct now; //for dfs
  90.  
  91. bool canto(int i, int j) {
  92.     return i >= 0 && i < n && j >= 0 && j < m && mtr[i][j] == 1 && !was[i][j];
  93. }
  94.  
  95. void dfs(int i, int j) {
  96.     was[i][j] = 1;
  97.     now.upd(i, j, n, m);
  98.     vector <int> di = {-1, 0, 1, 0}, dj = {0, 1, 0, -1};
  99.     for (int to = 0; to < 4; ++to) {
  100.         int toi = i + di[to], toj = j + dj[to];
  101.         if (canto(toi, toj)) {
  102.             dfs(toi, toj);
  103.         }
  104.     }
  105. }
  106.  
  107. rct getrec(vector <vector <int> > a) {
  108.     rct res = {inf, -1, inf, -1};
  109.     if (sh) {
  110.         cout << "GETREC\n";
  111.     }
  112.     for (int i = 0; i < n; ++i) {
  113.         for (int j = 0; j < n; ++j) {
  114.             if (a[i][j] == 0) {
  115.                 res.upd(i, j, n, n);
  116.             }
  117.         }
  118.         if (sh) {
  119.             //cout << "I = " << i << "\n";
  120.             //cout << "res\n";
  121.             //res.see();
  122.             //cout << "\n";
  123.         }
  124.     }
  125.     return res;
  126. }
  127.  
  128. bool in(rct rec, int i, int j) {
  129.     int xj = j;
  130.     int yi = n - 1 - i;
  131.     bool r =  xj >= rec.x1
  132.     && xj <= rec.x2
  133.     && yi >= rec.y1
  134.     && yi <= rec.y2;
  135.     if (r == 1) {
  136.         cout << "\nCHECK IN\n";
  137.         cout << "i j = " << i << ' ' << j << "\n";
  138.         cout << "rec\n";
  139.         rec.see();
  140.         cout << "yi xj = " << yi << ' ' << xj << "\n";
  141.     }
  142.  
  143.     return r;
  144. }
  145.  
  146. bool noout(vector <vector <int> > a, int clr, rct rec) {
  147.     //n m global ok???
  148.     for (int i = 0; i < n; ++i) {
  149.         for (int j = 0; j < m; ++j) {
  150.             if (a[i][j] == clr) {
  151.                 if (!in(rec, i, j)) {
  152.                     return 0;
  153.                 }
  154.             }
  155.         }
  156.     }
  157.     return 1;
  158. }
  159. bool gen = 0;
  160.  
  161.  
  162. void getmtr(vector <vector <int> > a) {
  163.     rct rec = getrec(a);
  164.     if (sh) {
  165.         cout << "rec\n";
  166.         rec.see();
  167.     }
  168.     int lx = rec.x2 - rec.x1 + 1;
  169.     int ly = rec.y2 - rec.y1 + 1;
  170.  
  171.     mtr.resize(ly, vector <int> (lx, 99));
  172.     int fri = n - rec.y2;
  173.     int toi = fri + ly - 1;
  174.     fri--;
  175.     toi--;
  176.     int frj = rec.x1;
  177.     int toj = rec.x2;
  178.  
  179.     if (sh) {
  180.         cout << "fri toi = " << fri << ' ' << toi << "\n";
  181.         cout << "frj toj = " << frj << ' ' << toj << "\n";
  182.     }
  183.  
  184.     int x = -1;
  185.     for (int i = fri; i <= toi; ++i) {
  186.         x++;
  187.         int y = -1;
  188.         for (int j = frj; j <= toj; ++j) {
  189.             y++;
  190.             mtr[x][y] = a[i][j];
  191.         }
  192.     }
  193.     n = ly;
  194.     m = lx;
  195.     //new, n m for needed matrix
  196. }
  197.  
  198.  
  199. /*
  200. 13
  201. .............
  202. .............
  203. .............
  204. ..#######....
  205. ..#######....
  206. ..###........
  207. ..###........
  208. ..###........
  209. ..###........
  210. ..#######....
  211. ..#######....
  212. .............
  213. .............
  214. */
  215.  
  216. bool Csee = 0;
  217. bool Hsee = 0;
  218. /*
  219. 10
  220. ..........
  221. ..##...#..
  222. ..##...#..
  223. ..##...#..
  224. ..##...#..
  225. ..######..
  226. ..##...#..
  227. ..##...#..
  228. ..........
  229. ..........
  230.  
  231. 10
  232. ..........
  233. ..##...#..
  234. ..##...#..
  235. ..##...#..
  236. ..##...#..
  237. ..######..
  238. ..##...#..
  239. ..##...#..
  240. ..........
  241. ..........
  242. */
  243.  
  244. bool cmp(rct a, rct b) {
  245.     return a.y1 < b.y1 || a.y1 == b.y1 && a.x1 < b.x1;
  246.     //a - left down, b - right up
  247. }
  248.  
  249. bool O(rct a, rct b) {
  250.     if (sh) {
  251.         cout << "a b\n";
  252.         a.see();
  253.         b.see();
  254.     }
  255.     bool ox = b.x1 > a.x1 && b.x2 < a.x2;
  256.     bool oy = b.y1 > a.y1 && b.y2 < a.y2;
  257.     return ox && oy;
  258. }
  259.  
  260. bool C(rct a, rct b) {
  261.     bool ox = b.x1 > a.x1 && b.x2 == a.x2;
  262.     bool oy = b.y1 > a.y1
  263.     && b.y2 < a.y2;
  264.     return ox && oy;
  265. }
  266.  
  267. bool L(rct a, rct b) {
  268.     bool ox = b.x1 > a.x1
  269.     && b.x2 == a.x2;
  270.     bool oy = b.y1 > a.y1
  271.     && b.y2 == a.y2;
  272.     return ox && oy;
  273. }
  274.  
  275. void OCL(rct a, rct b) {
  276.     if (sh) {
  277.         cout << "OCL\n";
  278.     }
  279.     if (O(a, b)) {
  280.         cout << 'O';
  281.         exit(0);
  282.     }
  283.     if (C(a, b)) {
  284.         cout << 'C';
  285.         exit(0);
  286.     }
  287.     if (L(a, b)) {
  288.         cout << 'L';
  289.         exit(0);
  290.     }
  291.     cout << 'X';
  292.     exit(0);
  293. }
  294.  
  295. bool H(rct a, rct b, rct c) {
  296.     bool ox =
  297.     b.x1 > a.x1
  298.     && b.x2 < a.x2
  299.     && b.x1 == c.x1
  300.     && b.x2 == c.x2;
  301.     bool oy =
  302.     b.y2 < c.y1
  303.     && c.y2 == a.y2
  304.     && b.y1 == a.y1;
  305.     return ox && oy;
  306. }
  307.  
  308. bool P(rct a, rct b, rct c) {
  309.     bool ox =
  310.     b.x1 > a.x1
  311.     && c.x1 > a.x1
  312.     && c.x2 < a.x2
  313.     && b.x2 == a.x2;
  314.     bool oy =
  315.     b.y1 == a.y1
  316.     && b.y2 < c.y1
  317.     && c.y2 < a.y2;
  318.     return ox && oy;
  319. }
  320.  
  321.  
  322. void HP(rct a, rct b, rct c) {
  323.     if (H(a, b, c)) {
  324.         cout << 'H';
  325.         exit(0);
  326.     }
  327.     if (P(a, b, c)) {
  328.         cout << 'P';
  329.         exit(0);
  330.     }
  331.     cout << 'X';
  332.     exit(0);
  333. }
  334. /*
  335. 2
  336. #.
  337. ##
  338.  
  339. 3
  340. ###
  341. #.#
  342. ###
  343.  
  344.  
  345. 5
  346. #####
  347. #..##
  348. #..##
  349. #####
  350. #####
  351. */
  352.  
  353.  
  354. int main() {
  355.     /*ios::sync_with_stdio(0);
  356.     cin.tie(0);
  357.     cout.tie(0);*/
  358.     //cin >> n;
  359.     //m = n;
  360.     if (Csee) {
  361.         vector <vector <char> > raw(13, vector <char> (13, '.'));
  362.         for (int i = 3; i <= 4; ++i) {
  363.             for (int j = 2; j <= 8; ++j) {
  364.                 raw[i][j] = '#';
  365.             }
  366.         }
  367.         for (int i = 5; i <= 8; ++i) {
  368.             for (int j = 2; j <= 4; ++j) {
  369.                 raw[i][j] = '#';
  370.             }
  371.         }
  372.         for (int i = 9; i <= 10; ++i) {
  373.             for (int j = 2; j <= 8; ++j) {
  374.                 raw[i][j] = '#';
  375.             }
  376.         }
  377.         cvch(raw);
  378.     }
  379.     if (Hsee) {
  380.         vector <vector <char> > raw(10, vector <char> (10, '.'));
  381.         for (int i = 1; i <= 7; ++i) {
  382.             for (int j = 2; j <= 3; ++j) {
  383.                 raw[i][j] = '#';
  384.             }
  385.             raw[i][7] = '#';
  386.         }
  387.         for (int j = 2; j <= 7; ++j) {
  388.             raw[5][j] = '#';
  389.         }
  390.         cvch(raw);
  391.     }
  392.  
  393.     cin >> n;
  394.     m = n;
  395.     vector <vector <int> > a(n, vector <int> (n, 99));
  396.     for (int i = 0; i < n; ++i) {
  397.         for (int j = 0; j < n; ++j) {
  398.             char k;
  399.             cin >> k;
  400.             if (k == '#') {
  401.                 a[i][j] = 0;
  402.             } else {
  403.                 a[i][j] = 1;
  404.             }
  405.         }
  406.     }
  407.     getmtr(a);
  408.     if (sh) {
  409.         cout << "mtr\n";
  410.         cvv(mtr);
  411.         cout << "n m = " << n << ' ' <<  m << "\n";
  412.     }
  413.     was.assign(n, vector <bool> (m, 0));
  414.     vector <rct> alrec;
  415.     for (int i = 0; i < n; ++i) {
  416.         for (int j = 0; j < m; ++j) {
  417.             if (mtr[i][j] == 1 && !was[i][j]) {
  418.                 now = {inf, -1, inf, -1};
  419.                 dfs(i, j);
  420.                 alrec.push_back(now);
  421.             }
  422.         }
  423.     }
  424.  
  425.     if (sh) {
  426.         cout << "alrec\n";
  427.         for (auto r: alrec) {
  428.             r.see();
  429.         }
  430.     }
  431.  
  432.     for (int i = 0; i < n; ++i) {
  433.         for (int j = 0; j < m; ++j) {
  434.             for (auto rec: alrec) {
  435.                 if (mtr[i][j] != 1 && in(rec, i, j)) {
  436.                     if (sh) {
  437.                         cout << "bad i j = " << i << ' ' << j << "\n";
  438.                     }
  439.                     cout << 'X';
  440.                     exit(0);
  441.                 }
  442.             }
  443.         }
  444.     }
  445.  
  446.     int recsz = alrec.size();
  447.     if (recsz > 2) {
  448.         cout << 'X';
  449.         exit(0);
  450.     }
  451.     if (recsz == 0) {
  452.         cout << 'I';
  453.         exit(0);
  454.     }
  455.     sort(alrec.begin(), alrec.end(), cmp);
  456.     if (sh) {
  457.         cout << "alrec sorted\n";
  458.         for (auto r: alrec) {
  459.             r.see();
  460.         }
  461.     }
  462.     if (recsz == 1) {
  463.         OCL({0, m - 1, 0, n - 1}, alrec[0]);
  464.         exit(0);
  465.     }
  466.     HP({0, m - 1, 0, n - 1}, alrec[0], alrec[1]);
  467.  
  468. }
  469.  
  470. /*
  471.  
  472. 4
  473. .##.
  474. .#..
  475. .##.
  476. ....
  477.  
  478. 4
  479. .##.
  480. .##.
  481. .##.
  482. ....
  483.  
  484. 2
  485. #.
  486. ##
  487.  
  488. 3
  489. ###
  490. #.#
  491. ###
  492.  
  493.  
  494. 13
  495. .............
  496. .............
  497. .............
  498. ..#######....
  499. ..#######....
  500. ..###........
  501. ..###........
  502. ..###........
  503. ..###........
  504. ..#######....
  505. ..#######....
  506. .............
  507. .............
  508.  
  509. 3
  510. ###
  511. #.#
  512. ###
  513.  
  514. /*
  515. 13
  516. .............
  517. .............
  518. .............
  519. ..#######....
  520. ..#######....
  521. ..###........
  522. ..###.#......
  523. ..###........
  524. ..###........
  525. ..#######....
  526. ..#######....
  527. .............
  528. .............
  529. */
  530.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement