Day8-9
This commit is contained in:
11
733. Flood Fill/733. Flood Fill.csproj
Normal file
11
733. Flood Fill/733. Flood Fill.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_733._Flood_Fill</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
14
733. Flood Fill/Program.cs
Normal file
14
733. Flood Fill/Program.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace _733._Flood_Fill;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int[][] matrix = new int[][] { new int[] { 1,1,1 },
|
||||
new int[] { 1,1,0 },
|
||||
new int[] { 1,0,1 } };
|
||||
var res = new Solution().FloodFill(matrix, 1,1, 2);
|
||||
foreach(var row in res)
|
||||
Console.WriteLine(String.Join(" ", row));
|
||||
}
|
||||
}
|
||||
32
733. Flood Fill/Solution.cs
Normal file
32
733. Flood Fill/Solution.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
namespace _733._Flood_Fill;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public int[][] FloodFill(int[][] image, int sr, int sc, int color)
|
||||
{
|
||||
int[][] res = new int[image.Length][];
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
res[i] = image[i].ToArray();
|
||||
|
||||
bool[][] colored = new bool[image.Length][];
|
||||
for (int i = 0; i < colored.Length; i++)
|
||||
colored[i] = new bool[image[i].Length];
|
||||
Recursion(res, sr, sc, res[sr][sc], color, colored);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void Recursion(int[][] image, int y, int x, int sourceColor, int toColor, bool[][] colored)
|
||||
{
|
||||
if (image[y][x] != sourceColor || colored[y][x])
|
||||
return;
|
||||
image[y][x] = toColor;
|
||||
colored[y][x] = true;
|
||||
for (int i = 1; i < 8; i += 2)
|
||||
{
|
||||
int nextY = i / 3 + y - 1;
|
||||
int nextX = i % 3 + x - 1;
|
||||
if (nextY >= 0 && nextY < image.Length && nextX >= 0 && nextX < image[nextY].Length)
|
||||
Recursion(image, nextY, nextX, sourceColor, toColor, colored);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user