-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0733-Flood-fill.cs
More file actions
37 lines (29 loc) · 1.02 KB
/
0733-Flood-fill.cs
File metadata and controls
37 lines (29 loc) · 1.02 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0733.Flood_fill
{
public class _0733_Flood_fill
{
public int[][] FloodFill(int[][] image, int sr, int sc, int newColor)
{
if (image[sr][sc] != newColor)
DFS(sr, sc, image, image[sr][sc], newColor);
return image;
}
private void DFS(int row, int col, int[][] image, int oldColor, int newColor)
{
if (image[row][col] != oldColor) return;
// change color to new
image[row][col] = newColor;
if (row > 0)
DFS(row - 1, col, image, oldColor, newColor); // top
if (row < image.Length - 1)
DFS(row + 1, col, image, oldColor, newColor); // bottom
if (col > 0)
DFS(row, col - 1, image, oldColor, newColor); // left
if (col < image[0].Length - 1)
DFS(row, col + 1, image, oldColor, newColor); // right
}
}
}