Home LeetCode - 733. Flood Fill
Post
Cancel

LeetCode - 733. Flood Fill

733. Flood Fill - easy

문제

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.

제한사항

  • 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
2
3
4
5
6
7
8
9
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.

풀이

  • DFS,BFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int n = image.size();
        int m = image[0].size();
        vector<vector<bool>> visited(n, vector<bool>(m, true));
        queue<pair<int, int>> q;
        
        // BFS
        q.push({sr, sc});
        while(!q.empty()){
            auto index = q.front();
            q.pop();
                
            // 현재 픽셀방문 체크
            visited[index.first][index.second] = false;
                
            // 현재 픽셀의 4방향 탐샘
            for(const auto& i : cardinal){
                // 4방향의 index 계산
                pair<int, int> newIndex = {index.first + i.first, index.second + i.second};
                
                // image의 index가 범위 이내이며,
                // 현재 픽셀과 같은 값을 가지며,
                // 방문한적이 없는 위치의 픽셀을 BFS로 탐색
                if( 0 <= newIndex.first && newIndex.first <= n - 1 && 
                    0 <= newIndex.second && newIndex.second <= m - 1 &&
                    image[index.first][index.second] == image[newIndex.first][newIndex.second] &&
                    visited[newIndex.first][newIndex.second])
                {
                    q.push(newIndex);    
                }
            }      
            image[index.first][index.second] = newColor;
        }

        return image;
    }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
    n := len(image)
    m := len(image[0])
    newImage := image
    
    checker := make([][]bool, n)
    for i := 0 ; i < n ; i++ {
        checker[i] = make([]bool, m)
    }
    
    offset := [][]int{ {-1, 0}, {1, 0}, {0, 1}, {0, -1} }
    queue := [][]int{ {sr, sc} }
    
    for len(queue) > 0 {
        index := queue[0]
        queue = queue[1:]
            
        checker[index[0]][index[1]] = true
        
        for _, i := range offset {
            newIndex := []int{ index[0] + i[0], index[1] + i[1] }
            
            if  (0 <= newIndex[0] && newIndex[0] < n) &&
                (0 <= newIndex[1] && newIndex[1] < m) &&
                (image[index[0]][index[1]] == image[newIndex[0]][newIndex[1]]) && 
                (!checker[newIndex[0]][newIndex[1]]) {
                queue = append(queue, newIndex)
            }
        }
        
        newImage[index[0]][index[1]] = newColor 
    }
    
    return newImage
}
This post is licensed under CC BY 4.0 by the author.