Advertisement
Korotkodul

N5 4

Oct 25th, 2022 (edited)
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.28 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 = m - 1 - i;
  131.     yi++;
  132.     bool r =  xj >= rec.x1
  133.     && xj <= rec.x2
  134.     && yi >= rec.y1
  135.     && yi <= rec.y2;
  136.     if (r == 1) {
  137.         cout << "\nCHECK IN\n";
  138.         cout << "i j = " << i << ' ' << j << "\n";
  139.         cout << "rec\n";
  140.         rec.see();
  141.         cout << "yi xj = " << yi << ' ' << xj << "\n";
  142.     }
  143.  
  144.     return r;
  145. }
  146.  
  147. bool noout(vector <vector <int> > a, int clr, rct rec) {
  148.     //n m global ok???
  149.     for (int i = 0; i < n; ++i) {
  150.         for (int j = 0; j < m; ++j) {
  151.             if (a[i][j] == clr) {
  152.                 if (!in(rec, i, j)) {
  153.                     return 0;
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     return 1;
  159. }
  160. bool gen = 0;
  161.  
  162.  
  163. void getmtr(vector <vector <int> > a) {
  164.     rct rec = getrec(a);
  165.     if (sh) {
  166.         cout << "rec\n";
  167.         rec.see();
  168.     }
  169.     int lx = rec.x2 - rec.x1 + 1;
  170.     int ly = rec.y2 - rec.y1 + 1;
  171.  
  172.     mtr.resize(ly, vector <int> (lx, 99));
  173.     int fri = n - rec.y2;
  174.     int toi = fri + ly - 1;
  175.     fri--;
  176.     toi--;
  177.     int frj = rec.x1;
  178.     int toj = rec.x2;
  179.  
  180.     if (sh) {
  181.         cout << "fri toi = " << fri << ' ' << toi << "\n";
  182.         cout << "frj toj = " << frj << ' ' << toj << "\n";
  183.     }
  184.  
  185.     int x = -1;
  186.     for (int i = fri; i <= toi; ++i) {
  187.         x++;
  188.         int y = -1;
  189.         for (int j = frj; j <= toj; ++j) {
  190.             y++;
  191.             mtr[x][y] = a[i][j];
  192.         }
  193.     }
  194.     n = ly;
  195.     m = lx;
  196.     //new, n m for needed matrix
  197. }
  198.  
  199.  
  200. /*
  201. 13
  202. .............
  203. .............
  204. .............
  205. ..#######....
  206. ..#######....
  207. ..###........
  208. ..###........
  209. ..###........
  210. ..###........
  211. ..#######....
  212. ..#######....
  213. .............
  214. .............
  215. */
  216.  
  217. bool Csee = 0;
  218. bool Hsee = 0;
  219. /*
  220. 10
  221. ..........
  222. ..##...#..
  223. ..##...#..
  224. ..##...#..
  225. ..##...#..
  226. ..######..
  227. ..##...#..
  228. ..##...#..
  229. ..........
  230. ..........
  231.  
  232. 10
  233. ..........
  234. ..##...#..
  235. ..##...#..
  236. ..##...#..
  237. ..##...#..
  238. ..######..
  239. ..##...#..
  240. ..##...#..
  241. ..........
  242. .........#
  243. */
  244.  
  245. int main() {
  246.     /*ios::sync_with_stdio(0);
  247.     cin.tie(0);
  248.     cout.tie(0);*/
  249.     //cin >> n;
  250.     //m = n;
  251.     if (Csee) {
  252.         vector <vector <char> > raw(13, vector <char> (13, '.'));
  253.         for (int i = 3; i <= 4; ++i) {
  254.             for (int j = 2; j <= 8; ++j) {
  255.                 raw[i][j] = '#';
  256.             }
  257.         }
  258.         for (int i = 5; i <= 8; ++i) {
  259.             for (int j = 2; j <= 4; ++j) {
  260.                 raw[i][j] = '#';
  261.             }
  262.         }
  263.         for (int i = 9; i <= 10; ++i) {
  264.             for (int j = 2; j <= 8; ++j) {
  265.                 raw[i][j] = '#';
  266.             }
  267.         }
  268.         cvch(raw);
  269.     }
  270.     if (Hsee) {
  271.         vector <vector <char> > raw(10, vector <char> (10, '.'));
  272.         for (int i = 1; i <= 7; ++i) {
  273.             for (int j = 2; j <= 3; ++j) {
  274.                 raw[i][j] = '#';
  275.             }
  276.             raw[i][7] = '#';
  277.         }
  278.         for (int j = 2; j <= 7; ++j) {
  279.             raw[5][j] = '#';
  280.         }
  281.         cvch(raw);
  282.     }
  283.  
  284.     cin >> n;
  285.     m = n;
  286.     vector <vector <int> > a(n, vector <int> (n, 99));
  287.     for (int i = 0; i < n; ++i) {
  288.         for (int j = 0; j < n; ++j) {
  289.             char k;
  290.             cin >> k;
  291.             if (k == '#') {
  292.                 a[i][j] = 0;
  293.             } else {
  294.                 a[i][j] = 1;
  295.             }
  296.         }
  297.     }
  298.     getmtr(a);
  299.     if (sh) {
  300.         cout << "mtr\n";
  301.         cvv(mtr);
  302.         cout << "n m = " << n << ' ' <<  m << "\n";
  303.     }
  304.     was.assign(n, vector <bool> (m, 0));
  305.     vector <rct> alrec;
  306.     for (int i = 0; i < n; ++i) {
  307.         for (int j = 0; j < m; ++j) {
  308.             if (mtr[i][j] == 1 && !was[i][j]) {
  309.                 now = {inf, -1, inf, -1};
  310.                 dfs(i, j);
  311.                 alrec.push_back(now);
  312.             }
  313.         }
  314.     }
  315.  
  316.     if (sh) {
  317.         cout << "alrec\n";
  318.         for (auto r: alrec) {
  319.             r.see();
  320.         }
  321.     }
  322.  
  323.     for (int i = 0; i < n; ++i) {
  324.         for (int j = 0; j < m; ++j) {
  325.             for (auto rec: alrec) {
  326.                 if (mtr[i][j] != 1 && in(rec, i, j)) {
  327.                     if (sh) {
  328.                         cout << "bad i j = " << i << ' ' << j << "\n";
  329.                     }
  330.                     cout << 'X';
  331.                     exit(0);
  332.                 }
  333.             }
  334.         }
  335.     }
  336. }
  337.  
  338. /*
  339. 13
  340. .............
  341. .............
  342. .............
  343. ..#######....
  344. ..#######....
  345. ..###........
  346. ..###........
  347. ..###........
  348. ..###........
  349. ..#######....
  350. ..#######....
  351. .............
  352. .............
  353.  
  354. /*
  355. 13
  356. .............
  357. .............
  358. .............
  359. ..#######....
  360. ..#######....
  361. ..###........
  362. ..###.#......
  363. ..###........
  364. ..###........
  365. ..#######....
  366. ..#######....
  367. .............
  368. .............
  369. */
  370.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement