Day8-9
This commit is contained in:
11
200. Number of Islands/200. Number of Islands.csproj
Normal file
11
200. Number of Islands/200. Number of Islands.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_200._Number_of_Islands</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
200. Number of Islands/Program.cs
Normal file
9
200. Number of Islands/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _200._Number_of_Islands;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
42
200. Number of Islands/Solution.cs
Normal file
42
200. Number of Islands/Solution.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace _200._Number_of_Islands;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public int NumIslands(char[][] grid)
|
||||
{
|
||||
int[][] was = new int[grid.Length][];
|
||||
for (int i = 0; i < grid.Length; i++)
|
||||
was[i] = new int[grid[i].Length];
|
||||
|
||||
int curId = 0;
|
||||
for (int y = 0; y < grid.Length; y++)
|
||||
{
|
||||
for (int x = 0; x < grid[y].Length; x++)
|
||||
{
|
||||
if (was[y][x] == 0 && grid[y][x] == '1')
|
||||
curId++;
|
||||
else
|
||||
continue;
|
||||
|
||||
Recursion(grid, was, y, x, curId);
|
||||
}
|
||||
}
|
||||
return curId;
|
||||
}
|
||||
|
||||
private void Recursion(char[][] grid, int[][] was, int y, int x, int id)
|
||||
{
|
||||
if (was[y][x]!=0 || grid[y][x] != '1')
|
||||
return;
|
||||
was[y][x] = id;
|
||||
for (int i = 1; i < 8; i += 2)
|
||||
{
|
||||
int nextY = i / 3 + y - 1;
|
||||
int nextX = i % 3 + x - 1;
|
||||
if (nextY >= 0 && nextY < was.Length && nextX >= 0 && nextX < was[nextY].Length)
|
||||
Recursion(grid, was, nextY, nextX, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user