An image
is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).ios
Given a coordinate (sr, sc)
representing the starting pixel (row and column) of the flood fill, and a pixel value newColor
, "flood fill" the image.ide
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.spa
At the end, return the modified image.code
Example 1:orm
Input: image = [[1,1,1],[1,1,0],[1,0,1]] sr = 1, sc = 1, newColor = 2 Output: [[2,2,2],[2,2,0],[2,0,1]] Explanation: From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected by a path of the same color as the starting pixel are colored with the new color. Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
Note:blog
image
and image[0]
will be in the range [1, 50]
.0 <= sr < image.length
and 0 <= sc < image[0].length
.image[i][j]
and newColor
will be an integer in [0, 65535]
.解法1rem
#include<string> #include<vector> #include<iostream> #include<unordered_map> #include<unordered_set> using namespace std; class Solution { void helper(vector<vector<bool>>& visited,vector<vector<int>>& image, int sr, int sc, int newColor,int color){ if(visited[sr][sc]) return; //printf("process %d %d\n",sr,sc); visited[sr][sc]=true; image[sr][sc]=newColor; vector<pair<int,int>> directions{{-1,0},{1,0},{0,-1},{0,1}}; for(auto direct: directions){ int sr_new=sr+direct.first; int sc_new=sc+direct.second; if(sr_new>=0&&sr_new<image.size()&&sc_new>=0&&sc_new<image[0].size()) if(image[sr_new][sc_new]==color) helper(visited,image,sr_new,sc_new,newColor,color); } } public: vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) { int h=image.size(); int w=image[0].size(); int color=image[sr][sc]; vector<vector<bool>> visited(h,vector<bool>(w,false)); helper(visited,image,sr,sc,newColor,color); return image; } }; int main(){ vector<vector<int>> image{{1,1,1},{1,1,0},{1,0,1}}; int sr=1,sc=1,newColor=2; Solution solution; solution.floodFill(image,sr,sc,newColor); for(auto r: image){ for(auto c: r){ cout<<c<<" "; } cout<<endl; } }
解法2string
#include<string> #include<vector> #include<iostream> #include<unordered_map> #include<unordered_set> using namespace std; class Solution { void dfs(vector<vector<int>>& image, int sr, int sc, int newColor,int color){ if(image[sr][sc]==newColor|image[sr][sc]!=color){ return; } image[sr][sc]=newColor; vector<pair<int,int>> directions{{-1,0},{1,0},{0,-1},{0,1}}; for(auto direct: directions){ int sr_new=sr+direct.first; int sc_new=sc+direct.second; if(sr_new>=0&&sr_new<image.size()&&sc_new>=0&&sc_new<image[0].size()&&image[sr_new][sc_new]==color) dfs(image,sr_new,sc_new,newColor,color); } } public: vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) { int h=image.size(); int w=image[0].size(); int color=image[sr][sc]; // vector<vector<bool>> visited(h,vector<bool>(w,false)); dfs(image,sr,sc,newColor,color); return image; } }; int main(){ vector<vector<int>> image{{1,1,1},{1,1,0},{1,0,1}}; int sr=1,sc=1,newColor=2; Solution solution; solution.floodFill(image,sr,sc,newColor); for(auto r: image){ for(auto c: r){ cout<<c<<" "; } cout<<endl; } }