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>_695._Max_Area_of_Island</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,10 @@
namespace _695._Max_Area_of_Island;
internal class Program
{
static void Main(string[] args)
{
int[][] matrix = {new int[] {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},new int[] {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},new int[] {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},new int[] {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}};
Console.WriteLine(new Solution().MaxAreaOfIsland(matrix));
}
}

View File

@@ -0,0 +1,41 @@
namespace _695._Max_Area_of_Island;
public class Solution
{
public int MaxAreaOfIsland(int[][] grid)
{
bool[][] visited = new bool[grid.Length][];
for (int i = 0; i < visited.Length; i++)
visited[i] = new bool[grid[i].Length];
int max = 0;
for (int y = 0; y < grid.Length; y++)
{
for (int x = 0; x < grid[y].Length; x++)
{
if (grid[y][x] == 0)
continue;
int curCount = 0;
Recursion(grid, y, x, ref curCount, visited);
max = Math.Max(max, curCount);
}
}
return max;
}
private void Recursion(int[][] grid, int y, int x, ref int count, bool[][] visited)
{
if (visited[y][x] || grid[y][x] == 0)
return;
visited[y][x] = true;
count++;
for (int i = 1; i < 8; i += 2)
{
int nextY = i / 3 + y - 1;
int nextX = i % 3 + x - 1;
if (nextY >= 0 && nextY < grid.Length && nextX >= 0 && nextX < grid[nextY].Length)
Recursion(grid, nextY, nextX, ref count, visited);
}
}
}