https://leetcode.com/problems/flood-fill/ Easy

Решение

class Solution {
    fun floodFill(image: Array<IntArray>, sr: Int, sc: Int, color: Int): Array<IntArray> {
        val originalColor = image[sr][sc]
        if (originalColor == color) return image

        val m = image.size
        val n = image[0].size
        val directions = arrayOf(
            intArrayOf(0, 1),
            intArrayOf(1, 0),
            intArrayOf(0, -1),
            intArrayOf(-1, 0)
        )

        fun dfs(r: Int, c: Int) {
            if (r < 0 || r >= m || c < 0 || c >= n || image[r][c] != originalColor) return
            image[r][c] = color
            for (dir in directions) {
                dfs(r + dir[0], c + dir[1])
            }
        }

        dfs(sr, sc)
        return image
    }
}