欢迎来到PJCK的博客

(BFS DFS 图的遍历) leetcode 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].

额,这个要审题。。。。其实这是一个基础题,BFS模板题,也是DFS模板题。。。,另外,我觉得最好都用这两个方法,因为它们都是很重要的,必须掌握的。

我先用BFS解决这个问题:

int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
class Solution {
public:
    typedef pair<int,int> pii;
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        queue<pii> q;
        
        int m = image.size();
        int n = image[0].size();
        if(m == 0 || n == 0)
            return {};
        vector<vector<bool>> vis(m,vector<bool>(n,false));
        q.push(make_pair(sr,sc));
        int target = image[sr][sc];   //审题!!!!!!!!
        image[sr][sc] = newColor;
        vis[sr][sc] = true;
        while(!q.empty()){
            auto t = q.front(); q.pop();
            for(int i = 0; i < 4; i++){
                int x = t.first + dx[i];
                int y = t.second + dy[i];
                if(x >= 0 && x < m && y >= 0 && y < n && image[x][y] == target && !vis[x][y]){
                    q.push(make_pair(x,y));
                    vis[x][y] = true;
                    image[x][y] = newColor;
                }
            }
        }
        return image;
    }
};

 

如果要用DFS的方法的话,先看官方题解:https://leetcode.com/articles/flood-fill/  

下面是我自己写的DFS:

class Solution {
public:
    void dfs(vector<vector<int>> &image, int r, int c, int newColor,int color){
        int m = image.size();
        int n = image[0].size();
        if(r >= 0 && r < m && c >= 0 && c < n && image[r][c] == color){
            image[r][c] = newColor;
            dfs(image,r+1,c,newColor,color);
            dfs(image,r-1,c,newColor,color);
            dfs(image,r,c+1,newColor,color);
            dfs(image,r,c-1,newColor,color);
        }
    }
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int m = image.size();
        int n = image[0].size();
        if(m == 0 || n == 0) return {};
        int color = image[sr][sc];
        if(color != newColor){
            dfs(image,sr,sc,newColor,color);
        }
        return image;
    }
};

 

posted @ 2019-05-16 20:58  PJCK  阅读(37)  评论(0编辑  收藏