zoukankan      html  css  js  c++  java
  • 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;
        }
    
    }
     
     
  • 相关阅读:
    hbase 相关
    java 连接 hiveserver2 例子
    ik_max_word ik_smart 区别 和 单字 查询 不到问题
    如何计算地址线和数据线
    golang consistent hash 菜鸟分析
    借助GitHub托管你的项目代码
    PHP性能追踪及分析工具xhprof的安装与使用
    git使用方法
    go开发
    go的websocket实现
  • 原文地址:https://www.cnblogs.com/learning-c/p/9859283.html
Copyright © 2011-2022 走看看