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;
        }
    
    }
     
     
  • 相关阅读:
    Windows下图文详解PHP三种运行方式(php_mod、cgi、fastcgi)
    【强烈推荐】利用NAT、Host-Only双虚拟网卡,实现Virtual Box中CentOS6.3联网
    PHP批量清空删除指定文件夹内容
    MySQL收藏
    Eclipse快捷键与使用技巧总结
    “知乎网”技术方案初探
    PHP数组常用函数
    PHP常用字符串的操作函数
    Linux下,如何给PHP安装pdo_mysql扩展
    PHP二维数组排序(list_order)
  • 原文地址:https://www.cnblogs.com/learning-c/p/9859283.html
Copyright © 2011-2022 走看看