• 733. flood fill


    An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

    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.

    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.

    At the end, return the modified image.

    Example 1:

    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:

    • The length of image and image[0] will be in the range [1, 50].
    • The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
    • The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

     解法1

    #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
    ",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;
        }
    
    }

      解法2

    #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;
        }
    
    }
     
     
  • 相关阅读:
    POJ1204:Word Puzzles——题解
    HDU2222:Keywords Search——题解
    CF17E:Palisection——题解
    BZOJ2565:最长双回文串——题解
    POJ3974:Palindrome——题解
    接口测试的另一种方式 – 接口测试平台
    接口测试的另一种方式 – 接口测试平台
    接口测试的另一种方式 – 接口测试平台
    美团招聘-测试开发工程师
    美团招聘-测试开发工程师
  • 原文地址:https://www.cnblogs.com/learning-c/p/9859283.html
Copyright © 2020-2023  润新知