Advertisement
Josif_tepe

Untitled

Sep 25th, 2023
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1.  class Solution {
  2. public:
  3.     bool visited[55][55];
  4.     vector<vector<int>> mat;
  5.     const int di[4] = {0, 0, -1, 1};
  6.     const int dj[4] = {-1, 1, 0, 0};
  7.     int s_color, col, n, m;
  8.     void dfs(int ci, int cj) {
  9.         for(int i = 0; i < 4; i++) {
  10.             int ti = ci + di[i];
  11.             int tj = cj + dj[i];
  12.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] == s_color) {
  13.                 visited[ti][tj] = true;
  14.                 mat[ti][tj] = col;
  15.                 dfs(ti, tj);
  16.             }
  17.         }
  18.     }
  19.    
  20.     vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
  21.        
  22.         mat = image;
  23.         n = image.size();
  24.         m = image[0].size();
  25.         s_color = image[sr][sc];
  26.         col = color;
  27.         dfs(sr, sc);
  28.         mat[sr][sc] = col;
  29.         return mat;
  30.     }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement