Hey guys I need some troubleshooting with a C# task using BFS

I have a task that I's mostly working but sometimes I have undesirable outputs and not behaving right I didnt wanted to spam the chats so I asked here the task goes:
The input parameter n denotes a number that determines the size of the matrix.
E.g. n=3 means there is a 3x3 matrix.
There are Pokemon on certain fields in the matrix.
The input parameter x denotes an array of integers representing the x coordinates of the Pokemon in the matrix.
The input parameter y denotes an array of integers representing the y coordinates of the Pokemon in the matrix.
Fill and return the matrix so that each cell represents the minimum distance to the nearest Pokemon.
Distance is calculated only vertically and horizontally. Diagonal distance is presented as e.g. one horizontal and one vertical (2 in total).
Example 1:

n=3
x=[1]
y=[1]

The result
2,1,2
1,0,1
2,1,2

Example 2:
n=5
x=[1,4]
y=[1,3]

The result

2,1,2,3,3
1,0,1,2,2
2,1,2,2,1
3,2,2,1,0
4,3,3,2,1
The input parameter n denotes a number that determines the size of the matrix.
E.g. n=3 means there is a 3x3 matrix.
There are Pokemon on certain fields in the matrix.
The input parameter x denotes an array of integers representing the x coordinates of the Pokemon in the matrix.
The input parameter y denotes an array of integers representing the y coordinates of the Pokemon in the matrix.
Fill and return the matrix so that each cell represents the minimum distance to the nearest Pokemon.
Distance is calculated only vertically and horizontally. Diagonal distance is presented as e.g. one horizontal and one vertical (2 in total).
Example 1:

n=3
x=[1]
y=[1]

The result
2,1,2
1,0,1
2,1,2

Example 2:
n=5
x=[1,4]
y=[1,3]

The result

2,1,2,3,3
1,0,1,2,2
2,1,2,2,1
3,2,2,1,0
4,3,3,2,1
And the solution is in C# and in the chat It was too long to send: the problem for it doesn't output the second example this is the example
this is the output on the second example:
Example 2:
2 1 2 3 4
1 0 1 2 3
2 1 2 2 3
3 2 2 1 2
3 2 1 0 1
the result should be:
2,1,2,3,3
1,0,1,2,2
2,1,2,2,1
3,2,2,1,0
4,3,3,2,1
this is the output on the second example:
Example 2:
2 1 2 3 4
1 0 1 2 3
2 1 2 2 3
3 2 2 1 2
3 2 1 0 1
the result should be:
2,1,2,3,3
1,0,1,2,2
2,1,2,2,1
3,2,2,1,0
4,3,3,2,1
Thank you for any help in advance just trying to learn something advance💟
2 Replies
aligatorimatori
aligatorimatoriOP12mo ago
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();

}
}
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);
}
phaseshift
phaseshift12mo ago
In a 2D array, you can think of the left index as the row and the right index as the column. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/arrays#multidimensional-arrays So that doesn't match with "Pokémon x coord is horizontal index*
Want results from more Discord servers?
Add your server