2022. 7. 17. 20:42ㆍSTUDY/LeetCode
DFS.........문제다....... ㅠㅅㅠ;
재귀......다 !
생각해보면 Ea--------sy한 문제.......?
이해는 요 아래 동영상 보고 이해했다.
Question )
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.
Example 1:
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) 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.
Example 2:
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
Explanation: The starting pixel is already colored 0, so no changes are made to the image.
Constraints:
- m == image.length
- n == image[i].length
- 1 <= m, n <= 50
- 0 <= image[i][j], color < 216
- 0 <= sr < m
- 0 <= sc < n
오호...
우선 예시에서 주어진 sr/sc 값을 보자. (1,1) 좌표의 값은 1이다. 고로 source 값은 1이다. 요걸 color 값인 2로 변경하는거다.
source 값 1은 고로 color 값인 2하고 동일하다고 보는거임.
그러면.. 시작점이 어디인가? 음 ... sr/sc 값으로 하는것같군...?
거기를 기준으로 LEFT / RIGHT / DOWN / UP 에 위치한 좌표의 값을 확인해서, source값인 1이랑 같으면 color 값인 2로 변경해 주는거다. 만약 1이 아니면 다시 되돌아오고, 다른 direction으로 이동해서 확인한다. 이걸 모든 좌표에서 반복.
void paint(int **image, int imageSize, int*imageColSize, int sr, int sc, int newColor, int oldColor) {
if( sr < 0 || sc >= imageSize) return;
if( sc < 0 || sr >= imageSize) return;
if( image[sr][sc] == oldColor ) {
paint(image, imageSize, imageColSize, sr -1, sc, newColor, oldColor);
paint(image, imageSize, imageColSize, sr +1, sc, newColor, oldColor);
paint(image, imageSize, imageColSize, sr, sc -1, newColor, oldColor);
paint(image, imageSize, imageColSize, sr, sc +1, newColor, oldColor);
}
}
int** floodFill ( int ** image, int imageSize, int* imageColSize, int sr, int sc, int newColor, int* returnSize, int** returnColumnSizes)
{
int i;
*returnSize = iamgeSize;
*returnColumnSizes = malloc(sizeof(int)*imageSize);
for(i = 0; i < imageSize; i++) { // imageSize : 3..
(*returnColumnSizes)[i] = imageColSize[i];
}
if(newColor != image[sr][sc])
paint(image, imageSize, imageColSize, sr,sc, newColor, image[sr][sc]);
return image;
}
^_ㅠㅜ......... 와메 ... ㅋㅋㅋ역시 자꾸 풀어봐야 되는구나.... !
'STUDY > LeetCode' 카테고리의 다른 글
LeetCode - 509.Fibonacci Num & 1137. N-th Tribonacci Number (1) | 2022.07.25 |
---|---|
LeetCode - 240. Search a 2D Matrix II (0) | 2022.07.24 |
LeetCode - 19. Remove Nth Node From End of List (1) | 2022.07.16 |
LeetCode - 876. Middle of the Linked List (0) | 2022.07.16 |
LeetCode - Reverse Words in a String III (0) | 2022.07.16 |