-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode733.cpp
46 lines (40 loc) · 1.26 KB
/
Leetcode733.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
using namespace std;
/* https://leetcode.com/problems/flood-fill/description/ */
class Solution {
public:
vector<vector<int>> temp;
int c;
int prev;
int count = 0;
void update(int i,int j){
if(count == (temp.size() * temp[0].size())) return;
temp[i][j] = c;
count++;
cout<<i<<" "<<j<<" "<<prev<<" "<<temp[i][j]<<endl;
if(i-1>=0 && prev == temp[i-1][j]){
update(i-1,j);
if(count == (temp.size() * temp[0].size())) return;
}
if(i+1<temp.size() && prev == temp[i+1][j]){
update(i+1,j);
if(count == (temp.size() * temp[0].size())) return;
}
if(j-1>=0 && prev == temp[i][j-1]){
update(i,j-1);
if(count == (temp.size() * temp[0].size())) return;
}
if(j+1<temp[0].size() && prev == temp[i][j+1]){
update(i,j+1);
if(count == (temp.size() * temp[0].size())) return;
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
temp.resize(image.size(),vector<int> (image[0].size()));
temp = image;
prev = image[sr][sc];
c = color;
update(sr,sc);
return temp;
}
};