The flood fill algorithm is somewhat tricky to implement. I use a iterative method instead of recursive flooding. I first scan through the west and east points to the current point until a different colored one is encountered. then, the points between the west and east boundary are colored. finally, the very north and south points to these points that with the same color are putted in a queue. repeat these steps for each point in the queue wocould do the job.
When coloring the points horizontally, points in the queue may be colored as well. thus, if a point is already colored when it is dequeued, there is no need to check for it at all. I missed this point at first, thus making my program run more slowly than the recursive version.
I also implement a recursive version of the flood fill algorithm. when others claim that the recursive wocould make the stack overflow or take a longer time to run. to my deep surprise, though I have tried my best to optimize the iterative version, the recursive version gets a better run time and does not cause a run time error on the judge.
Code:
- /*************************************** **********************************
- * Copyright (c) 2008 by liukaipeng *
- * Liukaipeng at gmail dot com *
- **************************************** *********************************/
- /* @ Judge_id 00000 10267 C "Graphical Editor "*/
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Include <strings. h>
- # Deprecision Max 252
- /*------------------------------------------------*
- * Queue implementation *
- *------------------------------------------------*/
- # Define queue_max (max * max)
- Typedef struct point
- {
- Int X;
- Int y;
- } Point;
- Typedef point elemtype;
- Typedef struct queue {
- Elemtype data [queue_max];
- Int front;
- Int tail;
- Int size;
- } Queue;
- Void Init (queue * q ){
- Q-> front = 0;
- Q-> tail = 0;
- Q-> size = 0;
- }
- Int empty (queue * q ){
- Return Q-> size = 0;
- }
- Void enqueue (queue * q, elemtype e ){
- Q-> data [q-> tail] = E;
- ++ Q-> size;
- + Q-> tail;
- Q-> tail % = queue_max;
- }
- Elemtype dequeue (queue * q ){
- Elemtype E = Q-> data [q-> front];
- -- Q-> size;
- + Q-> front;
- Q-> front % = queue_max;
- Return E;
- }
- /*------------------------------------------------*/
- Char bitmap [Max] [Max];
- Int xmax, Ymax;
- Void handle_c (char Param [])
- {
- Int y;
- Memset (bitmap, 'O', Max * max );
- For (y = 1; y <= Ymax; ++ y)
- Bitmap [y] [xmax + 1] = '/0 ';
- }
- Void handle_ I (char Param [])
- {
- Sscanf (Param, "% d", & xmax, & Ymax );
- Handle_c (PARAM );
- }
- Void handle_l (char Param [])
- {
- Int X, Y, C;
- Sscanf (Param, "% d % C", & X, & Y, & C );
- Bitmap [y] [x] = C;
- }
- Void handle_v (char Param [])
- {
- Int X, Y1, Y2, C;
- Sscanf (Param, "% d % C", & X, & Y1, & Y2, & C );
- If (Y1> Y2)
- Y1 ^ = Y2, Y2 ^ = Y1, Y1 ^ = Y2;
- For (; Y1 <= Y2; ++ Y1)
- Bitmap [Y1] [x] = C;
- }
- Void handle_h (char Param [])
- {
- Int x1, x2, Y, C;
- Sscanf (Param, "% d % C", & X1, & X2, & Y, & C );
- If (x1> x2)
- X1 ^ = x2, X2 ^ = x1, X1 ^ = x2;
- Memset (Bitmap [y] + X1, C, x2-x1 + 1 );
- }
- Void handle_k (char Param [])
- {
- Int X1, Y1, X2, Y2, C;
- Sscanf (Param, "% d % C", & X1, & Y1, & X2, & Y2, & C );
- If (x1> x2)
- X1 ^ = x2, X2 ^ = x1, X1 ^ = x2;
- If (Y1> Y2)
- Y1 ^ = Y2, Y2 ^ = Y1, Y1 ^ = Y2;
- For (; Y1 <= Y2; ++ Y1)
- Memset (Bitmap [Y1] + X1, C, x2-x1 + 1 );
- }
- Void flood (int x, int y, char o, char C)
- {
- If (C = O)
- Return;
- Bitmap [y] [x] = C;
- If (x-1> 0 & bitmap [y] [x-1] = O)
- Flood (x-1, Y, O, C );
- If (Y-1> 0 & bitmap [Y-1] [x] = O)
- Flood (x, Y-1, O, C );
- If (x + 1 <xmax + 1 & bitmap [y] [x + 1] = O)
- Flood (x + 1, Y, O, C );
- If (Y + 1 <Ymax + 1 & bitmap [Y + 1] [x] = O)
- Flood (X, Y + 1, O, C );
- }
- Void handle_f (char Param [])
- {
- Point P;
- Queue Q;
- Char O, C;
- Int W, E, N, S;
- Sscanf (Param, "% d % C", & P. X, & P. Y, & C );
- O = bitmap [P. Y] [p. x];
- /* Flood (P. X, p. Y, O, C );*/
- Init (& Q );
- Enqueue (& Q, P );
- While (! Empty (& Q )){
- P = dequeue (& Q );
- If (Bitmap [P. Y] [p. x] = C)
- Continue;
- For (W = p. x; W-1> 0 & bitmap [P. Y] [W-1] = O; -- W );
- For (E = p. x; e + 1 <xmax + 1 & bitmap [P. Y] [E + 1] = O; ++ E );
- Memset (Bitmap [P. Y] + W, C, E-W + 1 );
- N = Y-1, S = P. Y + 1;
- If (n> 0)
- For (P. Y = n, p. x = W; p. x <= E; ++ p. x)
- If (Bitmap [P. Y] [p. x] = O)
- Enqueue (& Q, P );
- If (S <Ymax + 1)
- For (P. Y = s, p. x = W; p. x <= E; ++ p. x)
- If (Bitmap [P. Y] [p. x] = O)
- Enqueue (& Q, P );
- }
- }
- Void handle_s (char Param [])
- {
- Printf ("% s/n", Param );
- Int X, Y;
- For (y = 1; y <= Ymax; ++ y ){
- Puts (Bitmap [y] + 1 );
- }
- }
- Int main (INT argc, char * argv [])
- {
- # Ifndef online_judge
- Char in [256];
- Char out [256];
- Strcpy (in, argv [0]);
- Strcat (in, ". In ");
- Freopen (in, "r", stdin );
- Strcpy (Out, argv [0]);
- Strcat (Out, ". Out ");
- Freopen (Out, "W", stdout );
- # Endif
- Char Buf [1024];
- For (scanf ("% [^/n]/n", Buf); Buf [0]! = 'X'; scanf ("% [^/n]/n", Buf )){
- Switch (BUF [0]) {
- Case 'I ':
- Handle_ I (BUF + 2 );
- Break;
- Case 'C ':
- Handle_c (BUF + 2 );
- Break;
- Case 'l ':
- Handle_l (BUF + 2 );
- Break;
- Case 'V ':
- Handle_v (BUF + 2 );
- Break;
- Case 'H ':
- Handle_h (BUF + 2 );
- Break;
- Case 'K ':
- Handle_k (BUF + 2 );
- Break;
- Case 'F ':
- Handle_f (BUF + 2 );
- Break;
- Case's ':
- Handle_s (BUF + 2 );
- Break;
- Case 'X ':
- Break;
- Default:
- Break;
- }
- }
- Return 0;
- }