Post

[코테] 733. Flood Fill

난이도:EasyFailed

Solution, failed to solve

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
        
        R, C = len(image), len(image[0])
        
        old_color = image[sr][sc]
        
        if old_color == color:
            return image

        def dfs(r, c):
            if image[r][c] != old_color:
                return
            
            image[r][c] = color
            
            if r - 1 >= 0: dfs(r - 1, c)
            if r + 1 < R:  dfs(r + 1, c)
            if c - 1 >= 0: dfs(r, c - 1)
            if c + 1 < C:  dfs(r, c + 1)
        
        dfs(sr, sc)
        
        return image
This post is licensed under CC BY 4.0 by the author.