This commit is contained in:
Electrominch
2022-10-11 01:45:16 +03:00
parent c019e8856c
commit 02afed9c4c
34 changed files with 691 additions and 3 deletions

View 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>

View 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));
}
}

View 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);
}
}
}