fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // M x N matrix
  5. #define M 10
  6. #define N 10
  7.  
  8. // Below arrays details all 8 possible movements
  9. int row[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
  10. int col[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
  11.  
  12. // check if it is possible to go to pixel (x, y) from
  13. // current pixel. The function returns false if the pixel
  14. // has different color or it is not a valid pixel
  15. bool isSafe(char mat[M][N], int x, int y, char target)
  16. {
  17. return x >= 0 && x < N && y >= 0 && y < M
  18. && mat[x][y] == target;
  19. }
  20.  
  21. // Flood fill using BFS
  22. void floodfill(char mat[M][N], int x, int y, char replacement)
  23. {
  24. // create a queue and enqueue starting pixel
  25. queue<pair<int, int>> q;
  26. q.push({x, y});
  27.  
  28. // get target color
  29. char target = mat[x][y];
  30.  
  31. // run till queue is not empty
  32. while (!q.empty())
  33. {
  34. // pop front node from queue and process it
  35. pair<int, int> node = q.front();
  36. q.pop();
  37.  
  38. // (x, y) represents current pixel
  39. int x = node.first, y = node.second;
  40.  
  41. // replace current pixel color with that of replacement
  42. mat[x][y] = replacement;
  43.  
  44. // process all 8 adjacent pixels of current pixel and
  45. // enqueue each valid pixel
  46. for (int k = 0; k < 8; k++)
  47. {
  48. // if adjacent pixel at position (x + row[k], y + col[k]) is
  49. // a valid pixel and have same color as that of current pixel
  50. if (isSafe(mat, x + row[k], y + col[k], target))
  51. {
  52. q.push({x + row[k], y + col[k]});
  53. }
  54. }
  55. }
  56. }
  57.  
  58. // main function
  59. int main()
  60. {
  61. // matrix showing portion of the screen having different colors
  62. char mat[M][N] =
  63. {
  64. { 'Y', 'Y', 'Y', 'G', 'G', 'G', 'G', 'G', 'G', 'G' },
  65. { 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'G', 'X', 'X', 'X' },
  66. { 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'X', 'X', 'X' },
  67. { 'W', 'W', 'W', 'W', 'W', 'G', 'G', 'G', 'G', 'X' },
  68. { 'W', 'R', 'R', 'R', 'R', 'R', 'G', 'X', 'X', 'X' },
  69. { 'W', 'W', 'W', 'R', 'R', 'G', 'G', 'X', 'X', 'X' },
  70. { 'W', 'B', 'W', 'R', 'R', 'R', 'R', 'R', 'R', 'X' },
  71. { 'W', 'B', 'B', 'B', 'B', 'R', 'R', 'X', 'X', 'X' },
  72. { 'W', 'B', 'B', 'X', 'B', 'B', 'B', 'B', 'X', 'X' },
  73. { 'W', 'B', 'B', 'X', 'X', 'X', 'X', 'X', 'X', 'X' }
  74. };
  75.  
  76. // start node
  77. int x = 3, y = 9; // target color = &quot;X&quot;
  78.  
  79. // replacement color
  80. char replacement = 'C';
  81.  
  82. // replace target color with replacement color
  83. floodfill(mat, x, y, replacement);
  84.  
  85. // print the colors after replacement
  86. for (int i = 0; i < M; i++)
  87. {
  88. for (int j = 0; j < N; j++)
  89. cout << setw(2) << mat[i][j];
  90.  
  91. cout << endl;
  92. }
  93. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
 Y Y Y G G G G G G G
 Y Y Y Y Y Y G C C C
 G G G G G G G C C C
 W W W W W G G G G C
 W R R R R R G C C C
 W W W R R G G C C C
 W B W R R R R R R C
 W B B B B R R C C C
 W B B C B B B B C C
 W B B C C C C C C C