-
[LeetCode] 733. Flood Fill c++Coding Test/LeetCode 2022. 12. 28. 20:45728x90
- 문제
An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.
You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc].
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), and so on. Replace the color of all of the aforementioned pixels with color.
Return the modified image after performing the flood fill.
image[sr][sc] 시작점에서 상하좌우 배열의 값이 시작점과 같다면 color값으로 변경하는 문제입니다.
- 문제 해결
class Solution { public: void dfs(vector<vector<int>>& image, int i, int j, int val, int newColor){ if( i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] == newColor || image[i][j] != val){ return; } image[i][j] = newColor; dfs(image, i-1, j, val, newColor); dfs(image, i+1, j, val, newColor); dfs(image, i, j-1, val, newColor); dfs(image, i, j+1, val, newColor); } vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) { int val = image[sr][sc]; dfs(image, sr, sc, val, color); return image; } };
728x90'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 116. Populating Next Right Pointers in Each Node c++ (0) 2023.01.02 [LeetCode] 695. Max Area of Island c++ (0) 2022.12.28 [LeetCode] 1876. Substrings of Size Three with Distinct Characters c++ (0) 2022.12.27 [LeetCode] 567. Permutation in String c++ (0) 2022.12.27 [LeetCode] 3. Longest Substring Without Repeating Characters c++ (0) 2022.12.27