Day i do not know
This commit is contained in:
11
542. 01 Matrix/542. 01 Matrix.csproj
Normal file
11
542. 01 Matrix/542. 01 Matrix.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_542._01_Matrix</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
15
542. 01 Matrix/Program.cs
Normal file
15
542. 01 Matrix/Program.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace _542._01_Matrix;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int[][] mat = new int[][] { new int[] { 0,0,0, },
|
||||
new int[] { 0,1,0, },
|
||||
new int[] { 1,1,1, },
|
||||
};
|
||||
var res = new Solution().UpdateMatrix(mat);
|
||||
foreach(var row in res)
|
||||
Console.WriteLine(String.Join(" ", row));
|
||||
}
|
||||
}
|
||||
35
542. 01 Matrix/Solution.cs
Normal file
35
542. 01 Matrix/Solution.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace _542._01_Matrix;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public int[][] UpdateMatrix(int[][] mat)
|
||||
{
|
||||
bool hasChanges = true;
|
||||
while(hasChanges)
|
||||
{
|
||||
hasChanges = false;
|
||||
for (int y = 0; y < mat.Length; y++)
|
||||
{
|
||||
for (int x = 0; x < mat[y].Length; x++)
|
||||
{
|
||||
if (mat[y][x] == 0)
|
||||
continue;
|
||||
int min = int.MaxValue;
|
||||
for (int i = 1; i < 9; i += 2)
|
||||
{
|
||||
int newY = i / 3 + y - 1;
|
||||
int newX = i % 3 + x - 1;
|
||||
if (newY >= 0 && newY < mat.Length && newX >= 0 && newX < mat[newY].Length)
|
||||
{
|
||||
min = Math.Min(min, mat[newY][newX]);
|
||||
}
|
||||
}
|
||||
if(mat[y][x] != min + 1)
|
||||
hasChanges = true;
|
||||
mat[y][x] = min + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return mat;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user