Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct point {
  4. int x;
  5. int y;
  6. } point;
  7.  
  8.  
  9.  
  10. void flood_fill(int N, int ar[N][N], point start) {
  11.  
  12. if (start.x < 0 || start.x >=N || start.y < 0 || start.y >=N) //doesn't change anything if encountering already filled or
  13. return;
  14. if (ar[start.y][start.x] !=0)
  15. return;
  16.  
  17.  
  18. if (ar[start.y][start.x]==0) //if the point is equal to 0 then set it to 1
  19. ar[start.y][start.x] = 1;
  20.  
  21.  
  22.  
  23. point right;
  24. if (start.x < N) { //it can move right then it does
  25. right.x = start.x + 1; right.y = start.y;}
  26. else { //if the point is within the array boundaries doesn't move past them
  27. right.x = start.x; right.y = start.y;}
  28.  
  29. point left;
  30. if (start.x > 0) {
  31. left.x = start.x - 1; left.y = start.y;}
  32. else {
  33. left.x = start.x; left.y = start.y;}
  34.  
  35. point up;
  36. if (start.y > 0) {
  37. up.x = start.x; up.y = start.y - 1; }
  38. else {
  39. up.x = start.x; up.y = start.y;}
  40.  
  41. point down;
  42. if (start.y < N+1) {
  43. down.x = start.x; down.y = start.y + 1; }
  44. else {
  45. down.x = start.x; down.y = start.y; }
  46.  
  47. flood_fill(N, ar, right); //recursively call each direction
  48. flood_fill(N, ar, left);
  49. flood_fill(N, ar, up);
  50. flood_fill(N, ar, down);
  51. }
  52.  
  53.  
  54. int main(void) {
  55.  
  56. point start;
  57. start.x = 3;
  58. start.y = 4;
  59.  
  60. int n = 7;
  61. int arr[7][7] = {{0, 0, 0, 2, 0, 0 ,0}, {0,0,0,2,0,0,0}, {0,0,2,0,0,0,0}, {2,2,0,0,0,2,2}, {0,0,0,0,2,0,0}, {0,0,0,2,0,0,0}, {0,0,0,2,0,0,0}};
  62.  
  63. for (int i = 0; i < n; i++) {
  64. for (int j = 0; j < n; j++) {
  65. printf("%d ", arr[i][j]);
  66. }
  67. printf("\n");
  68. }
  69.  
  70. flood_fill(n, arr, start);
  71. printf("\n\n");
  72.  
  73. for (int i = 0; i < n; i++) {
  74. for (int j = 0; j < n; j++) {
  75. printf("%d ", arr[i][j]);
  76. }
  77. printf("\n");
  78. }
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement