-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b551eac
commit 0a8c17c
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
public: | ||
|
||
void dfs(int sr,int sc,vector<vector<int>> &ans,vector<vector<int>>& image,int newcolor, int delrow[],int delcol[], int iniColor) | ||
{ | ||
int n=image.size(); | ||
int m=image[0].size(); | ||
ans[sr][sc]=newcolor; | ||
for(int i=0;i<4;i++) | ||
{ | ||
int nrow= sr+delrow[i]; | ||
int ncol= sc+delcol[i]; | ||
if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && image[nrow][ncol]==iniColor && ans[nrow][ncol]!=newcolor) | ||
dfs(nrow,ncol,ans,image,newcolor,delrow,delcol,iniColor); | ||
} | ||
} | ||
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) { | ||
|
||
int iniColor= image[sr][sc]; | ||
vector<vector<int>> ans = image; | ||
int delrow[]={-1,0,+1,0}; | ||
int delcol[]={0,+1,0,-1}; | ||
dfs(sr,sc,ans,image,color,delrow,delcol, iniColor); | ||
return ans; | ||
} | ||
}; |