UVa Problem Solution: 10267 - Graphical Editor


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 would 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. Many others claim that the recursive would 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. #define 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 %d", &xmax, &ymax);
  61.   handle_C(param);
  62. }
  63. void handle_L(char param[])
  64. {
  65.   int x, y, c;
  66.   sscanf(param, "%d %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 %d %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 %d %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 %d %d %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. void handle_F(char param[])
  111. {
  112.   point p;
  113.   queue q;
  114.   char o, c;
  115.   int w, e, n, s;
  116.   sscanf(param, "%d %d %c", &p.x, &p.y, &c);
  117.   o = bitmap[p.y][p.x];
  118.   /*  flood(p.x, p.y, o, c);*/
  119.   init(&q);
  120.   enqueue(&q, p);
  121.   while (!empty(&q)) {
  122.     p = dequeue(&q);
  123.     if (bitmap[p.y][p.x] == c)
  124.       continue;
  125.     for (w = p.x; w-1 > 0 && bitmap[p.y][w-1] == o; --w); 
  126.     for (e = p.x; e+1 < xmax+1 && bitmap[p.y][e+1] == o; ++e);
  127.     memset(bitmap[p.y]+w, c, e-w+1);
  128.     n = p.y-1, s = p.y+1;
  129.     if (n > 0)
  130.       for (p.y = n, p.x = w; p.x <= e; ++p.x)
  131.         if (bitmap[p.y][p.x] == o)
  132.           enqueue(&q, p);
  133.     if (s < ymax+1)
  134.       for (p.y = s, p.x = w; p.x <= e; ++p.x)
  135.         if (bitmap[p.y][p.x] == o)
  136.           enqueue(&q, p);
  137.   }
  138. }
  139. void handle_S(char param[])
  140. {
  141.   printf("%s/n", param);
  142.   int x, y;
  143.   for (y = 1; y <= ymax; ++y) {
  144.     puts(bitmap[y]+1);
  145.   }
  146. }
  147. int main(int argc, char *argv[])
  148. {
  149. #ifndef ONLINE_JUDGE
  150.   char in[256];
  151.   char out[256];
  152.   strcpy(in, argv[0]);
  153.   strcat(in, ".in");
  154.   freopen(in, "r", stdin);
  155.   strcpy(out, argv[0]);
  156.   strcat(out, ".out");
  157.   freopen(out, "w", stdout);
  158. #endif
  159.   char buf[1024];
  160.   for (scanf("%[^/n]/n", buf); buf[0] != 'X'; scanf("%[^/n]/n", buf)) {
  161.     switch(buf[0]) {
  162.     case 'I':
  163.       handle_I(buf+2);
  164.       break;
  165.     case 'C':
  166.       handle_C(buf+2);
  167.       break;
  168.     case 'L':
  169.       handle_L(buf+2);
  170.       break;
  171.     case 'V':
  172.       handle_V(buf+2);
  173.       break;
  174.     case 'H':
  175.       handle_H(buf+2);
  176.       break;
  177.     case 'K':
  178.       handle_K(buf+2);
  179.       break;
  180.     case 'F':
  181.       handle_F(buf+2);
  182.       break;
  183.     case 'S':
  184.       handle_S(buf+2);
  185.       break;
  186.     case 'X':
  187.       break;
  188.     default:
  189.       break;
  190.     }
  191.   }
  192.   return 0;
  193. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值