Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef COLOR_H
- #define COLOR_H
- #include <stdio.h>
- /**
- * Given n by m matrix (array[n][m]) finds adjacent entries with
- * the same color to replace it.
- * params:
- * n - number of rows in matrix
- * m - number of columns in matrix
- * arr - matrix to paint
- * i - row for current entry being processed
- * j - column for current entry being processed
- * ol - old color to find in matrix and replace
- * nw - new color to replace old color
- * touched - array to check if current entry was already processed
- **/
- int _paint
- (int n, int m, int *arr, int i, int j, int ol, int nw, int *touched)
- {
- int curind = n * i + j; //index of current entry
- if(i < 0 || i > n || j < 0 || j > n) {//index out of bounds
- return 1;
- }
- if(arr[curind] != ol || touched[curind]) {
- return 1;
- }
- touched[curind] = 1;
- arr[curind] = nw;
- return
- _paint(n, m, arr, i, j + 1, ol, nw, touched) &&
- _paint(n, m, arr, i, j - 1, ol, nw, touched) &&
- _paint(n, m, arr, i + 1, j, ol, nw, touched) &&
- _paint(n, m, arr, i - 1, j, ol, nw, touched) &&
- _paint(n, m, arr, i - 1, j - 1, ol, nw, touched) &&
- _paint(n, m, arr, i - 1, j + 1, ol, nw, touched) &&
- _paint(n, m, arr, i + 1, j - 1, ol, nw, touched) &&
- _paint(n, m, arr, i + 1, j + 1, ol, nw, touched);
- }
- int paint_bucket_fill
- (int n, int m, int *arr, int i, int j, int new_color)
- {
- int ind, touched[n * m];
- if(i < 0 || i > n || j < 0 || j > m) {
- fputs("invalid i j values.\n", stderr);
- return -1;
- }
- for(ind = 0; ind < (n * m); ++ind) {
- touched[ind] = 0;
- }
- int old_color = arr[n * i + j];
- return _paint(n, m, arr, i, j, old_color, new_color, touched);
- }
- #endif /* color.h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement