C
C#aligatorimatori

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💟
A
aligatorimatori160d 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);
}
P
phaseshift160d 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
More Posts
MVVM - View model with collection of view models?Within MVVM, can a view model have a collection of view models? Is it okay to do: ``` class BasketV2 QuestionsHi, I need to ask 2 questions as i am new to C#. So im missing 2 tasks to deploy my first app. And iRaw SQL Query QuestionI have a raw SQL query that I need to execute against a database that's external to my application. ✅ Help for unityGood evening I am looking for someone who can teach me how to create a game from A to Z. Even if I dAny way to get XAML Auto-complete in VS Code?I recognize this isn't quite a C# question, but it's related. I want to make cross platform apps usi.Net 8 Blazor Web App Identity Framework HelpSuper quick overview: I have a Blazor Web App that is single process only hosting UI. It communicaRegex Group Containshey simple easy question, but are there ways to access individual capture groups in a regex match? I"netsh" cmd Commands not being executedWell, I am trying to set up some firewall-rules and such, the problem is non of the commands is beinAutomate the process of transferring a Microsoft SQL Server .bak file to MongoDBI'm looking to automate the process of transferring a Microsoft SQL Server backup file (e.g., bulk.bMapping controller to / without breaking other controllersHi there, I would like to rename `HomeController` to `DefaultController` and instead of going to `/helpwhy is doesn't workTrack Thread/Task Progress through SignalR/WebSocketsI have an ASP.NET Web Core API and an Angular project both connected. In the dashboard of my Angula