Advertisement
Korotkodul

N5 8

Oct 25th, 2022
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.73 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 = 0;
  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. 1
  199. .
  200. */
  201.  
  202.  
  203. /*
  204. 13
  205. .............
  206. .............
  207. .............
  208. ..#######....
  209. ..#######....
  210. ..###........
  211. ..###........
  212. ..###........
  213. ..###........
  214. ..#######....
  215. ..#######....
  216. .............
  217. .............
  218. */
  219.  
  220. bool Csee = 0;
  221. bool Hsee = 0;
  222. /*
  223. 10
  224. ..........
  225. ..##...#..
  226. ..##...#..
  227. ..##...#..
  228. ..##...#..
  229. ..######..
  230. ..##...#..
  231. ..##...#..
  232. ..........
  233. ..........
  234.  
  235. 10
  236. ..........
  237. ..##...#..
  238. ..##...#..
  239. ..##...#..
  240. ..##...#..
  241. ..######..
  242. ..##...#..
  243. ..##...#..
  244. ..........
  245. ..........
  246. */
  247.  
  248. bool cmp(rct a, rct b) {
  249.     return a.y1 < b.y1 || a.y1 == b.y1 && a.x1 < b.x1;
  250.     //a - left down, b - right up
  251. }
  252.  
  253. bool O(rct a, rct b) {
  254.     if (sh) {
  255.         cout << "a b\n";
  256.         a.see();
  257.         b.see();
  258.     }
  259.     bool ox = b.x1 > a.x1 && b.x2 < a.x2;
  260.     bool oy = b.y1 > a.y1 && b.y2 < a.y2;
  261.     return ox && oy;
  262. }
  263.  
  264. bool C(rct a, rct b) {
  265.     bool ox = b.x1 > a.x1 && b.x2 == a.x2;
  266.     bool oy = b.y1 > a.y1
  267.     && b.y2 < a.y2;
  268.     return ox && oy;
  269. }
  270.  
  271. bool L(rct a, rct b) {
  272.     bool ox = b.x1 > a.x1
  273.     && b.x2 == a.x2;
  274.     bool oy = b.y1 > a.y1
  275.     && b.y2 == a.y2;
  276.     return ox && oy;
  277. }
  278.  
  279. void OCL(rct a, rct b) {
  280.     if (sh) {
  281.         cout << "OCL\n";
  282.     }
  283.     if (O(a, b)) {
  284.         cout << 'O';
  285.         exit(0);
  286.     }
  287.     if (C(a, b)) {
  288.         cout << 'C';
  289.         exit(0);
  290.     }
  291.     if (L(a, b)) {
  292.         cout << 'L';
  293.         exit(0);
  294.     }
  295.     cout << 'X';
  296.     exit(0);
  297. }
  298.  
  299. bool H(rct a, rct b, rct c) {
  300.     bool ox =
  301.     b.x1 > a.x1
  302.     && b.x2 < a.x2
  303.     && b.x1 == c.x1
  304.     && b.x2 == c.x2;
  305.     bool oy =
  306.     b.y2 < c.y1
  307.     && c.y2 == a.y2
  308.     && b.y1 == a.y1;
  309.     return ox && oy;
  310. }
  311.  
  312. bool P(rct a, rct b, rct c) {
  313.     bool ox =
  314.     b.x1 > a.x1
  315.     && c.x1 > a.x1
  316.     && c.x2 < a.x2
  317.     && b.x2 == a.x2;
  318.     bool oy =
  319.     b.y1 == a.y1
  320.     && b.y2 < c.y1
  321.     && c.y2 < a.y2;
  322.     return ox && oy;
  323. }
  324.  
  325.  
  326. void HP(rct a, rct b, rct c) {
  327.     if (H(a, b, c)) {
  328.         cout << 'H';
  329.         exit(0);
  330.     }
  331.     if (P(a, b, c)) {
  332.         cout << 'P';
  333.         exit(0);
  334.     }
  335.     cout << 'X';
  336.     exit(0);
  337. }
  338. /*
  339. 3
  340. .#.
  341. .#.
  342. .##
  343.  
  344. 3
  345. ###
  346. #.#
  347. ###
  348.  
  349.  
  350. 5
  351. #####
  352. #..##
  353. #..##
  354. ####.
  355. ####.
  356. */
  357.  
  358.  
  359. int main() {
  360.     /*ios::sync_with_stdio(0);
  361.     cin.tie(0);
  362.     cout.tie(0);*/
  363.     //cin >> n;
  364.     //m = n;
  365.     if (Csee) {
  366.         vector <vector <char> > raw(13, vector <char> (13, '.'));
  367.         for (int i = 3; i <= 4; ++i) {
  368.             for (int j = 2; j <= 8; ++j) {
  369.                 raw[i][j] = '#';
  370.             }
  371.         }
  372.         for (int i = 5; i <= 8; ++i) {
  373.             for (int j = 2; j <= 4; ++j) {
  374.                 raw[i][j] = '#';
  375.             }
  376.         }
  377.         for (int i = 9; i <= 10; ++i) {
  378.             for (int j = 2; j <= 8; ++j) {
  379.                 raw[i][j] = '#';
  380.             }
  381.         }
  382.         cvch(raw);
  383.     }
  384.     if (Hsee) {
  385.         vector <vector <char> > raw(10, vector <char> (10, '.'));
  386.         for (int i = 1; i <= 7; ++i) {
  387.             for (int j = 2; j <= 3; ++j) {
  388.                 raw[i][j] = '#';
  389.             }
  390.             raw[i][7] = '#';
  391.         }
  392.         for (int j = 2; j <= 7; ++j) {
  393.             raw[5][j] = '#';
  394.         }
  395.         cvch(raw);
  396.     }
  397.  
  398.     cin >> n;
  399.     m = n;
  400.     vector <vector <int> > a(n, vector <int> (n, 99));
  401.     int wh = 0;
  402.     for (int i = 0; i < n; ++i) {
  403.         for (int j = 0; j < n; ++j) {
  404.             char k;
  405.             cin >> k;
  406.             if (k == '#') {
  407.                 a[i][j] = 0;
  408.                 wh++;
  409.             } else {
  410.                 a[i][j] = 1;
  411.             }
  412.         }
  413.     }
  414.     if (wh == 0) {
  415.         cout << 'X';
  416.         exit(0);
  417.     }
  418.     getmtr(a);
  419.     if (sh) {
  420.         cout << "mtr\n";
  421.         cvv(mtr);
  422.         cout << "n m = " << n << ' ' <<  m << "\n";
  423.     }
  424.     was.assign(n, vector <bool> (m, 0));
  425.     vector <rct> alrec;
  426.     for (int i = 0; i < n; ++i) {
  427.         for (int j = 0; j < m; ++j) {
  428.             if (mtr[i][j] == 1 && !was[i][j]) {
  429.                 now = {inf, -1, inf, -1};
  430.                 dfs(i, j);
  431.                 alrec.push_back(now);
  432.             }
  433.         }
  434.     }
  435.  
  436.     if (sh) {
  437.         cout << "alrec\n";
  438.         for (auto r: alrec) {
  439.             r.see();
  440.         }
  441.     }
  442.  
  443.     for (int i = 0; i < n; ++i) {
  444.         for (int j = 0; j < m; ++j) {
  445.             for (auto rec: alrec) {
  446.                 if (mtr[i][j] != 1 && in(rec, i, j)) {
  447.                     if (sh) {
  448.                         cout << "bad i j = " << i << ' ' << j << "\n";
  449.                     }
  450.                     cout << 'X';
  451.                     exit(0);
  452.                 }
  453.             }
  454.         }
  455.     }
  456.  
  457.     int recsz = alrec.size();
  458.     if (recsz > 2) {
  459.         cout << 'X';
  460.         exit(0);
  461.     }
  462.     if (recsz == 0) {
  463.         cout << 'I';
  464.         exit(0);
  465.     }
  466.     sort(alrec.begin(), alrec.end(), cmp);
  467.     if (sh) {
  468.         cout << "alrec sorted\n";
  469.         for (auto r: alrec) {
  470.             r.see();
  471.         }
  472.     }
  473.     if (recsz == 1) {
  474.         OCL({0, m - 1, 0, n - 1}, alrec[0]);
  475.         exit(0);
  476.     }
  477.     HP({0, m - 1, 0, n - 1}, alrec[0], alrec[1]);
  478.  
  479. }
  480.  
  481. /*
  482.  
  483. 1
  484.  
  485. 3
  486. #.#
  487. ###
  488. #.#
  489.  
  490. 4
  491. ####
  492. #.##
  493. ####
  494. ##..
  495.  
  496. 5
  497. ####.
  498. #.##.
  499. ####.
  500. ##...
  501. .....
  502.  
  503. 4
  504. .##.
  505. .#..
  506. .##.
  507. ....
  508.  
  509. 4
  510. .##.
  511. .##.
  512. .##.
  513. ....
  514.  
  515. 2
  516. #.
  517. ##
  518.  
  519. 3
  520. ###
  521. #.#
  522. ###
  523.  
  524.  
  525. 13
  526. .............
  527. .............
  528. .............
  529. ..#######....
  530. ..#######....
  531. ..###........
  532. ..###........
  533. ..###........
  534. ..###........
  535. ..#######....
  536. ..#######....
  537. .............
  538. .............
  539.  
  540. 3
  541. ###
  542. #.#
  543. ###
  544.  
  545. /*
  546. 13
  547. .............
  548. .............
  549. .............
  550. ..#######....
  551. ..#######....
  552. ..###........
  553. ..###.#......
  554. ..###........
  555. ..###........
  556. ..#######....
  557. ..#######....
  558. .............
  559. .............
  560. */
  561.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement