Divi lev2
Define grid. If 0 is null, 1 indicates that there is an obstacle or you have considered it.
Flood fill records the number of flood fill cells in each format.
# Include <iostream> <br/> # include <queue> <br/> # include <vector> <br/> # include <cstring> <br/> # include <sstream> <br/> # include <algorithm> </P> <p> # define height400 <br/> # define width600 </P> <p> using namespace STD; </P> <p> typedef struct pixel {<br/> int X; <br/> int y; <br/>} pixel; </P> <p> const int xoffset [4] = {1, 0,-1, 0}; <br/> const int yoffset [4] = {0, 1, 0,-1 }; </P> <p> class grafixmask {<br/> priva Te: <br/> vector <int> holes; <br/> int grid [height] [width]; </P> <p> intisvalid (pixel N) <br/>{< br/> If (N. x <0 | n. x> height-1 | n. Y <0 | n. y> width-1 <br/> | grid [n. x] [n. y]) <br/> return 0; </P> <p> return 1; <br/>}</P> <p> public: <br/> vector <int> sortedareas (vector <string> rectangles) <br/> {<br/> memset (grid, 0, sizeof (INT) * height * width); </P> <p> for (INT I = 0; I <rectangles. size (); I ++ ){ <Br/> istringstream SS (rectangles [I]); <br/> int tlx, tly, brx, Bry; <br/> SS> tlx> tly> brx> Bry; <br/> for (INT x = tlx; x <= brx; X ++) <br/> for (INT y = tly; y <= Bry; y ++) <br/> grid [x] [Y] = 1; <br/>}</P> <p> for (INT x = 0; x <peight; X ++) <br/> for (INT y = 0; Y <width; y ++) {<br/> If (grid [x] [Y] = 1) <br/> continue; <br/> // cout <"x =" <x <"Y =" <Y <Endl; <br/> queue <pixel> q; <Br/> int CNT = 1; <br/> pixel P; <br/> P. X = x; <br/> P. y = y; <br/> grid [x] [Y] = 1; <br/> q. push (p); </P> <p> while (! Q. empty () {<br/> P = Q. front (); <br/> q. pop (); <br/> pixel newp; <br/> for (INT I = 0; I <4; I ++) {<br/> newp. X = P. X + xoffset [I]; <br/> newp. y = P. Y + yoffset [I]; </P> <p> If (isvalid (newp) {<br/> CNT ++; <br/> // cerr <CNT <Endl; <br/> grid [newp. x] [newp. y] = 1; <br/> q. push (newp); <br/>}< br/> holes. push_back (CNT); <br/>}< br/> sort (holes. begin (), holes. end (); <br/> return holes; <br/>}< br/>}; </P> <p> int main () <br/>{ <br/> grafixmask GM; </P> <p> // example 0 <br/> cout <"example 0:" <Endl; <br/> vector <string> VEC; <br/> Vec. push_back ("0 292 399 307"); </P> <p> Vec. push_back ("0 292 399 307"); <br/> vector <int> res = GM. sortedareas (VEC); </P> <p> return 0; <br/>}< br/>