aligatorimatori
aligatorimatori
CC#
Created by aligatorimatori on 12/6/2023 in #help
Hey guys I need some troubleshooting with a C# task using BFS
with example function:
public static void Main(string[] args)
{
Solution solution = new Solution();

// Example 1
int n1 = 3;
int[] x1 = { 1 };
int[] y1 = { 1 };
int[,] result1 = solution.Find(n1, x1, y1);
Console.WriteLine("Example 1:");
solution.PrintMatrix(result1);

// Example 2
int n2 = 5;
int[] x2 = { 1, 4 };
int[] y2 = { 1, 3 };
int[,] result2 = solution.Find(n2, x2, y2);
Console.WriteLine("\nExample 2:");
solution.PrintMatrix(result2);
}
public static void Main(string[] args)
{
Solution solution = new Solution();

// Example 1
int n1 = 3;
int[] x1 = { 1 };
int[] y1 = { 1 };
int[,] result1 = solution.Find(n1, x1, y1);
Console.WriteLine("Example 1:");
solution.PrintMatrix(result1);

// Example 2
int n2 = 5;
int[] x2 = { 1, 4 };
int[] y2 = { 1, 3 };
int[,] result2 = solution.Find(n2, x2, y2);
Console.WriteLine("\nExample 2:");
solution.PrintMatrix(result2);
}
6 replies
CC#
Created by aligatorimatori on 12/6/2023 in #help
Hey guys I need some troubleshooting with a C# task using BFS
using System;
using System.Collections.Generic;

public class Solution
{
public int[,] Find(int n, int[] x, int[] y)
{
int[,] matrix = new int[n, n];


for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
matrix[i, j] = int.MaxValue;
}
}


for (int i = 0; i < x.Length; i++)
{
int posX = x[i];
int posY = y[i];

matrix[posX, posY] = 0;
}


Queue<(int, int)> queue = new Queue<(int, int)>();

for (int i = 0; i < x.Length; i++)
{
int posX = x[i];
int posY = y[i];

queue.Enqueue((posX, posY));
}

int[] dx = { 0, 1, -1, 0 };
int[] dy = { 1, 0, 0, -1 };

while (queue.Count > 0)
{
(int currentX, int currentY) = queue.Dequeue();

for (int i = 0; i < 4; i++)
{
int newX = currentX + dx[i];
int newY = currentY + dy[i];

if (newX < 0 || newX >= n || newY < 0 || newY >= n)
{
continue;
}

if (matrix[newX, newY] > matrix[currentX, currentY] + 1)
{
matrix[newX, newY] = matrix[currentX, currentY] + 1;
queue.Enqueue((newX, newY));
}
}
}

return matrix;
}


public void PrintMatrix(int[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}


public static void Main(string[] args)
{
Solution solution = new Solution();

}
}
using System;
using System.Collections.Generic;

public class Solution
{
public int[,] Find(int n, int[] x, int[] y)
{
int[,] matrix = new int[n, n];


for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
matrix[i, j] = int.MaxValue;
}
}


for (int i = 0; i < x.Length; i++)
{
int posX = x[i];
int posY = y[i];

matrix[posX, posY] = 0;
}


Queue<(int, int)> queue = new Queue<(int, int)>();

for (int i = 0; i < x.Length; i++)
{
int posX = x[i];
int posY = y[i];

queue.Enqueue((posX, posY));
}

int[] dx = { 0, 1, -1, 0 };
int[] dy = { 1, 0, 0, -1 };

while (queue.Count > 0)
{
(int currentX, int currentY) = queue.Dequeue();

for (int i = 0; i < 4; i++)
{
int newX = currentX + dx[i];
int newY = currentY + dy[i];

if (newX < 0 || newX >= n || newY < 0 || newY >= n)
{
continue;
}

if (matrix[newX, newY] > matrix[currentX, currentY] + 1)
{
matrix[newX, newY] = matrix[currentX, currentY] + 1;
queue.Enqueue((newX, newY));
}
}
}

return matrix;
}


public void PrintMatrix(int[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}


public static void Main(string[] args)
{
Solution solution = new Solution();

}
}
6 replies