1. 程式人生 > >8.6-填充顏色

8.6-填充顏色

參考http://hawstein.com/posts/8.6.html

就像畫圖工具裡面的顏料桶,在指定範圍內(用綠色畫的邊界線)全部變為綠色。

#include <iostream>
#include <cstdio>
using namespace std;

enum color
{
    red, yellow, blue, green
};

void paint_fill(color screen[][5], int m, int n, int x, int y, color c)
{
    if(x<0 || x>=m || y<0 || y>=n)
        return;
    if(screen[x][y] == c)
        return;
    else
    {
        screen[x][y] = c;
        paint_fill(screen, m, n, x-1, y, c);
        paint_fill(screen, m, n, x+1, y, c);
        paint_fill(screen, m, n, x, y-1, c);
        paint_fill(screen, m, n, x, y+1, c);
    }
}
int main()
{
    color screen[5][5] =
    {
        {red, green, green, blue, yellow},
        {green, red, yellow, green, green},
        {green, green, red, blue, green},
        {red, green, green, green, yellow},
        {red, yellow, red, blue, yellow}
    };

    paint_fill(screen, 5, 5, 1, 2, (color)3);
    for(int i=0; i<5; ++i)
    {
        for(int j=0; j<5; ++j)
            cout<<screen[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}