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:
- /*************************************************************************
- * 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>
- #define 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 %d", &xmax, &ymax);
- handle_C(param);
- }
- void handle_L(char param[])
- {
- int x, y, c;
- sscanf(param, "%d %d %c", &x, &y, &c);
- bitmap[y][x] = c;
- }
- void handle_V(char param[])
- {
- int x, y1, y2, c;
- sscanf(param, "%d %d %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 %d %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 %d %d %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 %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 = p.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;
- }
本文介绍了一种使用迭代而非递归实现的洪水填充(flood fill)算法。通过扫描东西边界并改变颜色,然后将北部和南部相同颜色的点加入队列进行重复处理。文章对比了迭代与递归版本的性能,并提供了完整的代码示例。

被折叠的 条评论
为什么被折叠?



