This is actually a simple simulation question, but after several wa, I finally wrote the floodfill Algorithm in it, and I should be very careful when writing a correct floodfill algorithm.
The general process of floodfill is:
- Dye the start point and press it to the end of the team.
- Retrieve a point from the queueP X, Y)
- DetectionPThe surrounding area is (or, depending on whether the question requires eight or four directions), and the reachable points are dyed and pushed to the end of the team.
- Cycle 2 ~ 3 until the team is empty
I encountered several problems during implementation:
- Each vertex needs to be dyed before entering the team. If a vertex is not colored, it is detected from multiple directions and joined multiple times. This directly causes TLE.
- The most important thing is: Check whether the original color of the starting point is the same as that of the new color. If the color is the same, do not use floodfill; otherwise, the endless loop, TLE.
- The boundary check does not apply if the boundary is exceeded. Otherwise, the boundary check does not apply. Adding a circle around the Sentinel greatly simplifies the algorithm implementation.
Returns the volume CII index.
Returns the total index.
//////////////////////////////////////// /// // <Br/> // 10267-Graphical Editor <br/> // copyright (c) 2010 by Bo-Wen Feng <br/> // balonfan@gmail.com <br/> /////////////////////// //////////////////////////////////////// /// // </P> <p> # include <set> <br/> # include <map> <br/> # include <list> <br/> # include <queue> <br/> # include <stack> <br/> # include <vector> <br/> # include <String> <br/> # include <sstream> </P> <p> # include <cstdio> <br/> # include <cmath> <br/> # include <limits> <br/> # include <utility> </P> <p> # ifndef online_judge <br/> # include <fstream> <br/> STD:: ifstream CIN ("in.txt"); <br/> STD: ofstream cout ("out.txt"); <br/> // STD: ofstream cout (stdout ); <br/> # else <br/> # include <iostream> <br/> # endif </P> <p> typedef long llong; <br/> typedef unsigned long ullong; <Br/> typedef long double ldouble; </P> <p> using namespace STD; </P> <p> # define Max 255 <br/> # define max_name 20 </P> <p> char graph [Max] [Max]; <br/> int row, Col; </P> <p> void processi (int m, int N); <br/> void processc (); <br/> void processl (int x, int y, char C); <br/> void processv (int x, int Y1, int Y2, char C ); <br/> void processh (INT X1, int X2, int y, char C); <br/> void processk (INT X1, int Y1, int X2, Int Y2, char C); <br/> void processf (int x, int y, char C); <br/> void processs (char * Name ); </P> <p> void processi (int m, int N) <br/>{< br/> ROW = N; <br/> Col = m; </P> <p> processc (); <br/>}</P> <p> void processc () <br/> {<br/> for (INT x = 1; x <= Col; ++ X) <br/> for (INT y = 1; Y <= row; ++ y) <br/> graph [x] [Y] = 'O '; <br/>}</P> <p> void processl (int x, int y, char C) <br/>{< br/> graph [x] [Y] = C; <br/>}</P> <p> V Oid processv (int x, int Y1, int Y2, char c) <br/>{< br/> for (INT y = Y1; y <= Y2; ++ y) <br/> graph [x] [Y] = C; <br/>}</P> <p> void processh (INT X1, int X2, int y, char c) <br/>{< br/> for (INT x = x1; x <= x2; ++ X) <br/> graph [x] [Y] = C; <br/>}</P> <p> void processk (INT X1, int Y1, int X2, int Y2, char c) <br/>{< br/> for (INT x = x1; x <= x2; ++ X) <br/> processv (X, y1, Y2, c); <br/>}</P> <p> void pushpixels (que UE <pair <int, int> & flood, int x0, int y0, char orgcolor, char C) <br/> {<br/> If (graph [x0] [y0] = orgcolor) <br/>{< br/> graph [x0] [y0] = C; <br/> flood. push (make_pair (x0, y0); <br/>}</P> <p> void processf (int x, int y, char C) <br/>{< br/> char orgcolor = graph [x] [Y]; <br/> If (C = orgcolor) <br/>{< br/> return; <br/>}</P> <p> queue <pair <int, int> flood; </P> <p> flood. push (make_pair (x, y); </P> <p> gr APH [x] [Y] = C; </P> <p> while (! Flood. empty () <br/>{< br/> pair <int, int> P = flood. front (); <br/> int X0 = P. first; <br/> int Y0 = P. second; </P> <p> flood. pop (); </P> <p> If (x0 <= 0 | x0> Col | y0 <= 0 | y0> row) <br/> continue; </P> <p> pushpixels (flood, x0-1, y0, orgcolor, c); <br/> pushpixels (flood, x0 + 1, y0, orgcolor, c); <br/> pushpixels (flood, x0, y0-1, orgcolor, c); <br/> pushpixels (flood, x0, y0 + 1, orgcolor, c); <br/>} <Br/>}</P> <p> void processs (char * Name) <br/>{< br/> cout <name <'/N '; <br/> for (INT y = 1; y <= row; ++ y) <br/>{< br/> for (INT x = 1; x <= Col; ++ X) <br/> cout <graph [x] [Y]; </P> <p> cout <'/N '; <br/>}</P> <p> int main (INT argc, char * argv []) <br/>{< br/> char cmd; <br/> int X1, Y1, X2, Y2; <br/> char C; <br/> char name [max_name]; <br/> char skip [1025]; </P> <p> memset (graph,-1, sizeof (graph )); </P> <p> While (! Cin. EOF () <br/>{< br/> CIN> cmd; </P> <p> switch (CMD) <br/>{< br/> case 'I': <br/> CIN> x1> Y1; <br/> processi (x1, Y1 ); <br/> break; </P> <p> case 'C': <br/> processc (); <br/> break; </P> <p> case 'l': <br/> CIN> x1> Y1> C; <br/> processl (x1, Y1, C ); <br/> break; </P> <p> case 'V': <br/> CIN> x1> Y1> Y2> C; <br/> If (Y1> Y2) <br/> swap (Y1, Y2); <br/> processv (x1, Y1, Y2, C ); <br/> break; </P> <p> case 'H': <br/> CIN> x1> X2> Y1> C; <br/> If (x1> x2) <br/> swap (x1, x2); <br/> processh (x1, x2, Y1, C ); <br/> break; </P> <p> case 'K': <br/> CIN> x1> Y1> X2> Y2> C; <br/> If (Y1> Y2) <br/> swap (Y1, Y2); <br/> If (x1> x2) <br/> swap (x1, x2); <br/> processk (x1, Y1, X2, Y2, c); <br/> break; </P> <p> case 'F ': <br/> CIN> x1> Y1> C; <br/> processf (x1, Y1, c); <br/> break; </P> <p> case's ': <br/> CIN> name; <br/> processs (name); <br/> break; </P> <p> case 'X': <br/> break; </P> <p> default: <br/> cin. getline (skips, 1024); <br/> break; <br/>}</P> <p> If (cmd = 'X') <br/> break; <br/>}</P> <p> return 0; <br/>}</P> <p>