Paint Fill

/***
9.7
Implement the "paint fill" function that one might see on many image editing programs. That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color, fill in the surrounding area until the color changes from the original color.
***/

#include<iostream>

using namespace std;

void paintfill(int *a, int color, int org, int x, int y, int sz) {
       if(x < 0 || y < 0 || x > sz-1 || y > sz-1) return;
       if(*(a+x*sz+y) != org || *(a+x*sz+y) == color) return;
       *(a+x*sz+y) = color;
       paintfill(a, color, org, x-1, y, sz);
       paintfill(a, color, org, x+1, y, sz);
       paintfill(a, color, org, x, y-1, sz);
       paintfill(a, color, org, x, y+1, sz);
}

void main() {
       int a[] = {0, 0, 0, 0, 0, 0, 1, 0, 0};
       paintfill(a, 2, 0, 1, 1, 3);
       for (int i = 0; i<3; i++)
       {
              for (int j=0; j<3; j++)
              {
                     cout<<a[i*3+j]<<" ";
              }
              cout<<endl;
       }

}

Comments

Popular posts from this blog

Unique Binary Search Trees

Coin in Line