© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
166 replies
Spekulant

❔ index out of range or non-negative

class find_path
{
    public static int bfs(int[] start, int[] end,List<List<int>> board)
    {
        List<List<int>> Visited = new List<List<int>>();
        Queue<List<int> > queue = new Queue<List<int>>();
        
        Visited.Add(start.ToList());
        queue.Enqueue(start.ToList());

        while (queue.Count > 0)
        {
            var x = queue.Dequeue();
            List<int[]> possiblePositions = new List<int[]>();
            possiblePositions.Add(new int[] {x[0]-1, x[1], x[2]+1});
            possiblePositions.Add(new int[] {x[0]-1, x[1]-1, x[2]+1});
            possiblePositions.Add(new int[] {x[0]-1, x[1]+1, x[2]+1});
            possiblePositions.Add(new int[] {x[0]+1, x[1]-1, x[2]+1});
            possiblePositions.Add(new int[] {x[0]+1, x[1], x[2]+1});
            possiblePositions.Add(new int[] {x[0]+1, x[1]+1, x[2]+1});
            possiblePositions.Add(new int[] {x[0], x[1]+1, x[2]+1});
            possiblePositions.Add(new int[] {x[0], x[1]-1, x[2]+1});
            
            foreach (int[] position in possiblePositions)
            {
                if (position[0] > -1 && position[0] < 8 && position[1] > -1 && position[1] < 8)
                {
                    if (board[position[0]][position[1]] == 2)
                    {
                        return position[2];
                    }
                    if (board[position[0]][position[1]] != 1)
                    {
                        if (!Visited.Contains(position.Take(2).ToList()))
                        {
                            queue.Enqueue(position.ToList());
                            Visited.Add(position.Take(2).ToList());
                        }
                    }
                }
            }
        }
        
        return -1;
    }
}
class find_path
{
    public static int bfs(int[] start, int[] end,List<List<int>> board)
    {
        List<List<int>> Visited = new List<List<int>>();
        Queue<List<int> > queue = new Queue<List<int>>();
        
        Visited.Add(start.ToList());
        queue.Enqueue(start.ToList());

        while (queue.Count > 0)
        {
            var x = queue.Dequeue();
            List<int[]> possiblePositions = new List<int[]>();
            possiblePositions.Add(new int[] {x[0]-1, x[1], x[2]+1});
            possiblePositions.Add(new int[] {x[0]-1, x[1]-1, x[2]+1});
            possiblePositions.Add(new int[] {x[0]-1, x[1]+1, x[2]+1});
            possiblePositions.Add(new int[] {x[0]+1, x[1]-1, x[2]+1});
            possiblePositions.Add(new int[] {x[0]+1, x[1], x[2]+1});
            possiblePositions.Add(new int[] {x[0]+1, x[1]+1, x[2]+1});
            possiblePositions.Add(new int[] {x[0], x[1]+1, x[2]+1});
            possiblePositions.Add(new int[] {x[0], x[1]-1, x[2]+1});
            
            foreach (int[] position in possiblePositions)
            {
                if (position[0] > -1 && position[0] < 8 && position[1] > -1 && position[1] < 8)
                {
                    if (board[position[0]][position[1]] == 2)
                    {
                        return position[2];
                    }
                    if (board[position[0]][position[1]] != 1)
                    {
                        if (!Visited.Contains(position.Take(2).ToList()))
                        {
                            queue.Enqueue(position.ToList());
                            Visited.Add(position.Take(2).ToList());
                        }
                    }
                }
            }
        }
        
        return -1;
    }
}

im pretty sure that im checking if it ok but i dont know what im doing wrong
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

Weird error to do with index out of range
C#CC# / help
2y ago
System.IndexOutOfRangeException: "The index was out of range of the array." c# ReadWriteMemory
C#CC# / help
3y ago
❔ Index Out of Bounds Exception
C#CC# / help
3y ago