Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #ifndef COLOR_H
  2. #define COLOR_H
  3. #include <stdio.h>
  4.  
  5. /**
  6. * Given n by m matrix (array[n][m]) finds adjacent entries with
  7. * the same color to replace it.
  8. * params:
  9. * n - number of rows in matrix
  10. * m - number of columns in matrix
  11. * arr - matrix to paint
  12. * i - row for current entry being processed
  13. * j - column for current entry being processed
  14. * ol - old color to find in matrix and replace
  15. * nw - new color to replace old color
  16. * touched - array to check if current entry was already processed
  17. **/
  18. int _paint
  19. (int n, int m, int *arr, int i, int j, int ol, int nw, int *touched)
  20. {
  21. int curind = n * i + j; //index of current entry
  22.  
  23. if(i < 0 || i > n || j < 0 || j > n) {//index out of bounds
  24. return 1;
  25. }
  26.  
  27. if(arr[curind] != ol || touched[curind]) {
  28. return 1;
  29. }
  30.  
  31. touched[curind] = 1;
  32. arr[curind] = nw;
  33. return
  34. _paint(n, m, arr, i, j + 1, ol, nw, touched) &&
  35. _paint(n, m, arr, i, j - 1, ol, nw, touched) &&
  36. _paint(n, m, arr, i + 1, j, ol, nw, touched) &&
  37. _paint(n, m, arr, i - 1, j, ol, nw, touched) &&
  38. _paint(n, m, arr, i - 1, j - 1, ol, nw, touched) &&
  39. _paint(n, m, arr, i - 1, j + 1, ol, nw, touched) &&
  40. _paint(n, m, arr, i + 1, j - 1, ol, nw, touched) &&
  41. _paint(n, m, arr, i + 1, j + 1, ol, nw, touched);
  42.  
  43. }
  44.  
  45. int paint_bucket_fill
  46. (int n, int m, int *arr, int i, int j, int new_color)
  47. {
  48. int ind, touched[n * m];
  49.  
  50. if(i < 0 || i > n || j < 0 || j > m) {
  51. fputs("invalid i j values.\n", stderr);
  52. return -1;
  53. }
  54.  
  55. for(ind = 0; ind < (n * m); ++ind) {
  56. touched[ind] = 0;
  57. }
  58.  
  59. int old_color = arr[n * i + j];
  60. return _paint(n, m, arr, i, j, old_color, new_color, touched);
  61. }
  62.  
  63. #endif /* color.h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement