Advertisement
operaatoors

Flood-Fill algoritms

Mar 7th, 2016
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define M 9
  5. #define N 9
  6.  
  7. void color(int field[][N], int x, int y, int pc, int nc)
  8. {
  9.     if (x < 0 || x >= M || y < 0 || y >= N)
  10.         return;
  11.     if (field[x][y] != pc)
  12.         return;
  13.    
  14.     field[x][y] = nc;
  15.    
  16.     color(field, x+1, y, pc, nc);
  17.     color(field, x-1, y, pc, nc);
  18.     color(field, x, y+1, pc, nc);
  19.     color(field, x, y-1, pc, nc);
  20. }
  21.  
  22. void ff(int field[][N], int x, int y, int nc)
  23. {
  24.     int pc = field[x][y];
  25.     color(field, x, y, pc, nc);
  26. }
  27.  
  28. int main()
  29. {
  30.     int field[M][N] = {
  31.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  32.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  33.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  34.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  35.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  36.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  37.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  38.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  39.         {0, 0, 0, 0, 0, 0, 0, 0, 0},
  40.     };
  41.    
  42.     int x = 5, y = 5, nc = 1;
  43.     ff(field, x, y, nc);
  44.    
  45.     for (int i=0; i<M; i++){
  46.         for (int j=0; j<N; j++){
  47.             cout << field[i][j] << " ";
  48.         }
  49.         cout << endl;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement