Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool visited[55][55];
- vector<vector<int>> mat;
- const int di[4] = {0, 0, -1, 1};
- const int dj[4] = {-1, 1, 0, 0};
- int s_color, col, n, m;
- void dfs(int ci, int cj) {
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] == s_color) {
- visited[ti][tj] = true;
- mat[ti][tj] = col;
- dfs(ti, tj);
- }
- }
- }
- vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
- mat = image;
- n = image.size();
- m = image[0].size();
- s_color = image[sr][sc];
- col = color;
- dfs(sr, sc);
- mat[sr][sc] = col;
- return mat;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement