N (n <= 50) rectangle (coordinate in the upper left corner and coordinate in the lower right corner. The coordinate value may be 1e9.
Analysis: we can see the range of N and coordinates, and it is easy to think Of discretization. At that time, we did not expect how to determine the number of regions after discretization. When you look at other people's code, you can assign the vertex vis on the boundary to 1, then enumerate all vertices on the plane after hash, if you encounter a point with vis = 0, the search will continue from this point, and the search for the boundary will automatically stop, because the boundary vis = 1 and ANS ++ will be fine. It's really weak ..
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <vector> # include <map> using namespace STD; # define n 207 vector <int> VX, Vy; Map <int, int> HX, Hy; int DX [4] = {0, 0, 1,-1 }; int dy [4] = {1,-1, 0}; int vis [N] [N]; int L [55], R [55], t [55], B [55]; bool OK (INT NX, int NY) {If (nx> = 0 & NX <= 201 & ny> = 0 & ny <= 201 &&! Vis [NX] [NY]) return 1; return 0;} void DFS (int x, int y) {vis [x] [Y] = 1; for (int K = 0; k <4; k ++) {int Nx = x + dx [k]; int ny = Y + dy [k]; if (OK (NX, NY) DFS (NX, NY) ;}} int main () {int N, I, j; while (scanf ("% d ", & N )! = EOF & N) {VX. clear (); Vy. clear (); HX. clear (); Hy. clear (); for (I = 0; I <n; I ++) {scanf ("% d", & L [I], & T [I], & R [I], & B [I]); VX. push_back (L [I]); VX. push_back (R [I]); Vy. push_back (T [I]); Vy. push_back (B [I]);} Sort (VX. begin (), VX. end (); VX. erase (unique (VX. begin (), VX. end (), VX. end (); // uses STL to cleverly deduplicate sort (Vy. begin (), Vy. end (); Vy. erase (unique (Vy. begin (), Vy. end (), Vy. end (); for (I = 0; I <VX. size (); I ++) // The interval is 2 to prevent outgoing If the rectangle with the current area of 1 cannot be calculated, HX [VX [I] = 2 * I + 1; for (I = 0; I <Vy. size (); I ++) hy [Vy [I] = 2 * I + 1; for (I = 0; I <n; I ++) // discrete value {L [I] = HX [L [I]; t [I] = hy [T [I]; R [I] = HX [R [I]; B [I] = hy [B [I];} memset (VIS, 0, sizeof (VIS )); for (I = 0; I <n; I ++) // The boundary value {for (j = B [I]; j <= T [I]; j ++) {vis [J] [L [I] = 1; vis [J] [R [I] = 1;} For (j = L [I]; j <= R [I]; j ++) {vis [T [I] [J] = 1; vis [B [I] [J] = 1 ;}} int CNT = 0; for (I = 0; I <= 201; I ++) {for (J = 0; j <= 201; j ++) {If (! Vis [I] [J]) {CNT ++; DFS (I, j) ;}} printf ("% d \ n", CNT) ;}return 0 ;}View code