Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- bool sh = 0;
- void cvch(vector <vector <char> > a) {
- for (auto v: a) {
- for (auto i: v) {
- cout << i;
- } cout << "\n";
- } cout << "\n";
- }
- vector <vector <bool> > was;
- int n, m, inf = 2e9;
- vector <vector <int> > mtr;
- struct rct {
- int x1, x2, y1, y2;
- void upd(int i, int j, int n, int m) {
- int yi = n - 1 - i;
- int xj = j;
- if (sh) {
- //cout << "i j = " << i << ' ' << j << "\n";
- //cout << "yi xj = " << yi << ' ' << xj << "\n";
- }
- x1 = min(x1, xj);
- x2 = max(x2, xj);
- y1 = min(y1, yi);
- y2 = max(y2, yi);
- }
- void see() {
- cout << x1 << ' ' << x2 << ' ' << y1 << ' ' << y2 << "\n";
- }
- };
- rct now; //for dfs
- bool canto(int i, int j) {
- return i >= 0 && i < n && j >= 0 && j < m && mtr[i][j] == 1 && !was[i][j];
- }
- void dfs(int i, int j) {
- was[i][j] = 1;
- now.upd(i, j, n, m);
- vector <int> di = {-1, 0, 1, 0}, dj = {0, 1, 0, -1};
- for (int to = 0; to < 4; ++to) {
- int toi = i + di[to], toj = j + dj[to];
- if (canto(toi, toj)) {
- dfs(toi, toj);
- }
- }
- }
- rct getrec(vector <vector <int> > a) {
- rct res = {inf, -1, inf, -1};
- if (sh) {
- cout << "GETREC\n";
- }
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (a[i][j] == 0) {
- res.upd(i, j, n, n);
- }
- }
- if (sh) {
- //cout << "I = " << i << "\n";
- //cout << "res\n";
- //res.see();
- //cout << "\n";
- }
- }
- return res;
- }
- bool in(rct rec, int i, int j) {
- int xj = j;
- int yi = n - 1 - i;
- bool r = xj >= rec.x1
- && xj <= rec.x2
- && yi >= rec.y1
- && yi <= rec.y2;
- /*if (r == 1 ) {
- cout << "\nCHECK IN\n";
- cout << "i j = " << i << ' ' << j << "\n";
- cout << "rec\n";
- rec.see();
- cout << "yi xj = " << yi << ' ' << xj << "\n";
- }*/
- return r;
- }
- bool noout(vector <vector <int> > a, int clr, rct rec) {
- //n m global ok???
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- if (a[i][j] == clr) {
- if (!in(rec, i, j)) {
- return 0;
- }
- }
- }
- }
- return 1;
- }
- bool gen = 0;
- void getmtr(vector <vector <int> > a) {
- rct rec = getrec(a);
- if (sh) {
- cout << "rec\n";
- rec.see();
- }
- int lx = rec.x2 - rec.x1 + 1;
- int ly = rec.y2 - rec.y1 + 1;
- mtr.resize(ly, vector <int> (lx, 99));
- int fri = n - rec.y2;
- int toi = fri + ly - 1;
- fri--;
- toi--;
- int frj = rec.x1;
- int toj = rec.x2;
- if (sh) {
- cout << "fri toi = " << fri << ' ' << toi << "\n";
- cout << "frj toj = " << frj << ' ' << toj << "\n";
- }
- int x = -1;
- for (int i = fri; i <= toi; ++i) {
- x++;
- int y = -1;
- for (int j = frj; j <= toj; ++j) {
- y++;
- mtr[x][y] = a[i][j];
- }
- }
- n = ly;
- m = lx;
- //new, n m for needed matrix
- }
- /*
- 13
- .............
- .............
- .............
- ..#######....
- ..#######....
- ..###........
- ..###........
- ..###........
- ..###........
- ..#######....
- ..#######....
- .............
- .............
- */
- bool Csee = 0;
- bool Hsee = 0;
- /*
- 10
- ..........
- ..##...#..
- ..##...#..
- ..##...#..
- ..##...#..
- ..######..
- ..##...#..
- ..##...#..
- ..........
- ..........
- 10
- ..........
- ..##...#..
- ..##...#..
- ..##...#..
- ..##...#..
- ..######..
- ..##...#..
- ..##...#..
- ..........
- ..........
- */
- bool cmp(rct a, rct b) {
- return a.y1 < b.y1 || a.y1 == b.y1 && a.x1 < b.x1;
- //a - left down, b - right up
- }
- bool O(rct a, rct b) {
- if (sh) {
- cout << "a b\n";
- a.see();
- b.see();
- }
- bool ox = b.x1 > a.x1 && b.x2 < a.x2;
- bool oy = b.y1 > a.y1 && b.y2 < a.y2;
- return ox && oy;
- }
- bool C(rct a, rct b) {
- bool ox = b.x1 > a.x1 && b.x2 == a.x2;
- bool oy = b.y1 > a.y1
- && b.y2 < a.y2;
- return ox && oy;
- }
- bool L(rct a, rct b) {
- bool ox = b.x1 > a.x1
- && b.x2 == a.x2;
- bool oy = b.y1 > a.y1
- && b.y2 == a.y2;
- return ox && oy;
- }
- void OCL(rct a, rct b) {
- if (sh) {
- cout << "OCL\n";
- }
- if (O(a, b)) {
- cout << 'O';
- exit(0);
- }
- if (C(a, b)) {
- cout << 'C';
- exit(0);
- }
- if (L(a, b)) {
- cout << 'L';
- exit(0);
- }
- cout << 'X';
- exit(0);
- }
- bool H(rct a, rct b, rct c) {
- bool ox =
- b.x1 > a.x1
- && b.x2 < a.x2
- && b.x1 == c.x1
- && b.x2 == c.x2;
- bool oy =
- b.y2 < c.y1
- && c.y2 == a.y2
- && b.y1 == a.y1;
- return ox && oy;
- }
- bool P(rct a, rct b, rct c) {
- bool ox =
- b.x1 > a.x1
- && c.x1 > a.x1
- && c.x2 < a.x2
- && b.x2 == a.x2;
- bool oy =
- b.y1 == a.y1
- && b.y2 < c.y1
- && c.y2 < a.y2;
- return ox && oy;
- }
- void HP(rct a, rct b, rct c) {
- if (H(a, b, c)) {
- cout << 'H';
- exit(0);
- }
- if (P(a, b, c)) {
- cout << 'P';
- exit(0);
- }
- cout << 'X';
- exit(0);
- }
- /*
- 3
- .#.
- .#.
- .##
- 3
- ###
- #.#
- ###
- 5
- #####
- #..##
- #..##
- ####.
- ####.
- */
- int main() {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- //cin >> n;
- //m = n;
- if (Csee) {
- vector <vector <char> > raw(13, vector <char> (13, '.'));
- for (int i = 3; i <= 4; ++i) {
- for (int j = 2; j <= 8; ++j) {
- raw[i][j] = '#';
- }
- }
- for (int i = 5; i <= 8; ++i) {
- for (int j = 2; j <= 4; ++j) {
- raw[i][j] = '#';
- }
- }
- for (int i = 9; i <= 10; ++i) {
- for (int j = 2; j <= 8; ++j) {
- raw[i][j] = '#';
- }
- }
- cvch(raw);
- }
- if (Hsee) {
- vector <vector <char> > raw(10, vector <char> (10, '.'));
- for (int i = 1; i <= 7; ++i) {
- for (int j = 2; j <= 3; ++j) {
- raw[i][j] = '#';
- }
- raw[i][7] = '#';
- }
- for (int j = 2; j <= 7; ++j) {
- raw[5][j] = '#';
- }
- cvch(raw);
- }
- cin >> n;
- m = n;
- vector <vector <int> > a(n, vector <int> (n, 99));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- char k;
- cin >> k;
- if (k == '#') {
- a[i][j] = 0;
- } else {
- a[i][j] = 1;
- }
- }
- }
- getmtr(a);
- if (sh) {
- cout << "mtr\n";
- cvv(mtr);
- cout << "n m = " << n << ' ' << m << "\n";
- }
- was.assign(n, vector <bool> (m, 0));
- vector <rct> alrec;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- if (mtr[i][j] == 1 && !was[i][j]) {
- now = {inf, -1, inf, -1};
- dfs(i, j);
- alrec.push_back(now);
- }
- }
- }
- if (sh) {
- cout << "alrec\n";
- for (auto r: alrec) {
- r.see();
- }
- }
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- for (auto rec: alrec) {
- if (mtr[i][j] != 1 && in(rec, i, j)) {
- if (sh) {
- cout << "bad i j = " << i << ' ' << j << "\n";
- }
- cout << 'X';
- exit(0);
- }
- }
- }
- }
- int recsz = alrec.size();
- if (recsz > 2) {
- cout << 'X';
- exit(0);
- }
- if (recsz == 0) {
- cout << 'I';
- exit(0);
- }
- sort(alrec.begin(), alrec.end(), cmp);
- if (sh) {
- cout << "alrec sorted\n";
- for (auto r: alrec) {
- r.see();
- }
- }
- if (recsz == 1) {
- OCL({0, m - 1, 0, n - 1}, alrec[0]);
- exit(0);
- }
- HP({0, m - 1, 0, n - 1}, alrec[0], alrec[1]);
- }
- /*
- 1
- 3
- #.#
- ###
- #.#
- 4
- ####
- #.##
- ####
- ##..
- 5
- ####.
- #.##.
- ####.
- ##...
- .....
- 4
- .##.
- .#..
- .##.
- ....
- 4
- .##.
- .##.
- .##.
- ....
- 2
- #.
- ##
- 3
- ###
- #.#
- ###
- 13
- .............
- .............
- .............
- ..#######....
- ..#######....
- ..###........
- ..###........
- ..###........
- ..###........
- ..#######....
- ..#######....
- .............
- .............
- 3
- ###
- #.#
- ###
- /*
- 13
- .............
- .............
- .............
- ..#######....
- ..#######....
- ..###........
- ..###.#......
- ..###........
- ..###........
- ..#######....
- ..#######....
- .............
- .............
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement