Ultraviolet A Problem Solution: 10267-Graphical Editor

Source: Internet
Author: User

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:

  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 10267 C "Graphical Editor "*/
  6. # Include <stdio. h>
  7. # Include <stdlib. h>
  8. # Include <string. h>
  9. # Include <strings. h>
  10. # Deprecision Max 252
  11. /*------------------------------------------------*
  12. * Queue implementation *
  13. *------------------------------------------------*/
  14. # Define queue_max (max * max)
  15. Typedef struct point
  16. {
  17. Int X;
  18. Int y;
  19. } Point;
  20. Typedef point elemtype;
  21. Typedef struct queue {
  22. Elemtype data [queue_max];
  23. Int front;
  24. Int tail;
  25. Int size;
  26. } Queue;
  27. Void Init (queue * q ){
  28. Q-> front = 0;
  29. Q-> tail = 0;
  30. Q-> size = 0;
  31. }
  32. Int empty (queue * q ){
  33. Return Q-> size = 0;
  34. }
  35. Void enqueue (queue * q, elemtype e ){
  36. Q-> data [q-> tail] = E;
  37. ++ Q-> size;
  38. + Q-> tail;
  39. Q-> tail % = queue_max;
  40. }
  41. Elemtype dequeue (queue * q ){
  42. Elemtype E = Q-> data [q-> front];
  43. -- Q-> size;
  44. + Q-> front;
  45. Q-> front % = queue_max;
  46. Return E;
  47. }
  48. /*------------------------------------------------*/
  49. Char bitmap [Max] [Max];
  50. Int xmax, Ymax;
  51. Void handle_c (char Param [])
  52. {
  53. Int y;
  54. Memset (bitmap, 'O', Max * max );
  55. For (y = 1; y <= Ymax; ++ y)
  56. Bitmap [y] [xmax + 1] = '/0 ';
  57. }
  58. Void handle_ I (char Param [])
  59. {
  60. Sscanf (Param, "% d", & xmax, & Ymax );
  61. Handle_c (PARAM );
  62. }
  63. Void handle_l (char Param [])
  64. {
  65. Int X, Y, C;
  66. Sscanf (Param, "% d % C", & X, & Y, & C );
  67. Bitmap [y] [x] = C;
  68. }
  69. Void handle_v (char Param [])
  70. {
  71. Int X, Y1, Y2, C;
  72. Sscanf (Param, "% d % C", & X, & Y1, & Y2, & C );
  73. If (Y1> Y2)
  74. Y1 ^ = Y2, Y2 ^ = Y1, Y1 ^ = Y2;
  75. For (; Y1 <= Y2; ++ Y1)
  76. Bitmap [Y1] [x] = C;
  77. }
  78. Void handle_h (char Param [])
  79. {
  80. Int x1, x2, Y, C;
  81. Sscanf (Param, "% d % C", & X1, & X2, & Y, & C );
  82. If (x1> x2)
  83. X1 ^ = x2, X2 ^ = x1, X1 ^ = x2;
  84. Memset (Bitmap [y] + X1, C, x2-x1 + 1 );
  85. }
  86. Void handle_k (char Param [])
  87. {
  88. Int X1, Y1, X2, Y2, C;
  89. Sscanf (Param, "% d % C", & X1, & Y1, & X2, & Y2, & C );
  90. If (x1> x2)
  91. X1 ^ = x2, X2 ^ = x1, X1 ^ = x2;
  92. If (Y1> Y2)
  93. Y1 ^ = Y2, Y2 ^ = Y1, Y1 ^ = Y2;
  94. For (; Y1 <= Y2; ++ Y1)
  95. Memset (Bitmap [Y1] + X1, C, x2-x1 + 1 );
  96. }
  97. Void flood (int x, int y, char o, char C)
  98. {
  99. If (C = O)
  100. Return;
  101. Bitmap [y] [x] = C;
  102. If (x-1> 0 & bitmap [y] [x-1] = O)
  103. Flood (x-1, Y, O, C );
  104. If (Y-1> 0 & bitmap [Y-1] [x] = O)
  105. Flood (x, Y-1, O, C );
  106. If (x + 1 <xmax + 1 & bitmap [y] [x + 1] = O)
  107. Flood (x + 1, Y, O, C );
  108. If (Y + 1 <Ymax + 1 & bitmap [Y + 1] [x] = O)
  109. Flood (X, Y + 1, O, C );
  110. }
  111. Void handle_f (char Param [])
  112. {
  113. Point P;
  114. Queue Q;
  115. Char O, C;
  116. Int W, E, N, S;
  117. Sscanf (Param, "% d % C", & P. X, & P. Y, & C );
  118. O = bitmap [P. Y] [p. x];
  119. /* Flood (P. X, p. Y, O, C );*/
  120. Init (& Q );
  121. Enqueue (& Q, P );
  122. While (! Empty (& Q )){
  123. P = dequeue (& Q );
  124. If (Bitmap [P. Y] [p. x] = C)
  125. Continue;
  126. For (W = p. x; W-1> 0 & bitmap [P. Y] [W-1] = O; -- W );
  127. For (E = p. x; e + 1 <xmax + 1 & bitmap [P. Y] [E + 1] = O; ++ E );
  128. Memset (Bitmap [P. Y] + W, C, E-W + 1 );
  129. N = Y-1, S = P. Y + 1;
  130. If (n> 0)
  131. For (P. Y = n, p. x = W; p. x <= E; ++ p. x)
  132. If (Bitmap [P. Y] [p. x] = O)
  133. Enqueue (& Q, P );
  134. If (S <Ymax + 1)
  135. For (P. Y = s, p. x = W; p. x <= E; ++ p. x)
  136. If (Bitmap [P. Y] [p. x] = O)
  137. Enqueue (& Q, P );
  138. }
  139. }
  140. Void handle_s (char Param [])
  141. {
  142. Printf ("% s/n", Param );
  143. Int X, Y;
  144. For (y = 1; y <= Ymax; ++ y ){
  145. Puts (Bitmap [y] + 1 );
  146. }
  147. }
  148. Int main (INT argc, char * argv [])
  149. {
  150. # Ifndef online_judge
  151. Char in [256];
  152. Char out [256];
  153. Strcpy (in, argv [0]);
  154. Strcat (in, ". In ");
  155. Freopen (in, "r", stdin );
  156. Strcpy (Out, argv [0]);
  157. Strcat (Out, ". Out ");
  158. Freopen (Out, "W", stdout );
  159. # Endif
  160. Char Buf [1024];
  161. For (scanf ("% [^/n]/n", Buf); Buf [0]! = 'X'; scanf ("% [^/n]/n", Buf )){
  162. Switch (BUF [0]) {
  163. Case 'I ':
  164. Handle_ I (BUF + 2 );
  165. Break;
  166. Case 'C ':
  167. Handle_c (BUF + 2 );
  168. Break;
  169. Case 'l ':
  170. Handle_l (BUF + 2 );
  171. Break;
  172. Case 'V ':
  173. Handle_v (BUF + 2 );
  174. Break;
  175. Case 'H ':
  176. Handle_h (BUF + 2 );
  177. Break;
  178. Case 'K ':
  179. Handle_k (BUF + 2 );
  180. Break;
  181. Case 'F ':
  182. Handle_f (BUF + 2 );
  183. Break;
  184. Case's ':
  185. Handle_s (BUF + 2 );
  186. Break;
  187. Case 'X ':
  188. Break;
  189. Default:
  190. Break;
  191. }
  192. }
  193. Return 0;
  194. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.