C# BFS搜索时出错。索引超出范围。必须为非负数且小于集合的大小。参数名称:索引

C# BFS搜索时出错。索引超出范围。必须为非负数且小于集合的大小。参数名称:索引,c#,console-application,C#,Console Application,我有这个函数,将创建一个BFS。 我有一个函数,这是我的代码: static void Breadth_first_search(Queue<int> Q, List<int > trace, int[,] grid, int start, int nodes) { int u; List<int> visited = new List<int>(220000);

我有这个函数,将创建一个BFS。 我有一个函数,这是我的代码:

static void Breadth_first_search(Queue<int> Q, List<int > trace, int[,] grid, int start, int nodes)
        {
            int u;
            List<int> visited = new List<int>(220000);            
            Q.Enqueue(start);            
            trace[start] =  -1;            
            visited[start] = 1;
            do
            {
                u = Q.Peek();
                Q.Dequeue();
                for (int v = 1; v <= nodes; ++v)
                {
                    if ((grid[u, v] == 1) && visited[v] == 0)
                    {
                        Q.Enqueue(v);
                        trace[v] = u;
                        visited[v] = 1;
                    }
                }
            } while (Q.Count != 0);
        }
我在main中调用了函数:

static int Main()
        {

            List<int> trace = new List<int>(220);
            Queue<int> Q = new Queue<int>();
            int[,] grid = new int[582,582];                          

            int nodes;
            int vertices;
            Console.Write("Please input the number of Node : \n");

            nodes = Convert.ToInt32(Console.ReadLine());
            vertices = 200;
            Read_input_from_user(grid, vertices);
            int starting_position;
            int finishing_position;
            Console.Write("Please Input the Starting Node : \n");              
            starting_position = Convert.ToInt32(Console.ReadLine());
            Console.Write("Please Input the Finishing Node : \n");
            finishing_position = Convert.ToInt32(Console.ReadLine());
            Breadth_first_search(Q, trace, grid, starting_position, nodes);
            Trace_result(trace, starting_position, finishing_position, nodes);
            Console.ReadLine();
            return 0;
        }
static int Main()
{
列表跟踪=新列表(220);
队列Q=新队列();
整数[,]网格=新整数[582582];
int节点;
int顶点;
Console.Write(“请输入节点号:\n”);
nodes=Convert.ToInt32(Console.ReadLine());
顶点=200;
从用户处读取输入(网格、顶点);
int起始位置;
整饰位置;
Console.Write(“请输入起始节点:\n”);
起始位置=Convert.ToInt32(Console.ReadLine());
Console.Write(“请输入完成节点:\n”);
finishing_position=Convert.ToInt32(Console.ReadLine());
宽度优先搜索(Q、轨迹、网格、起始位置、节点);
跟踪结果(跟踪、起始位置、结束位置、节点);
Console.ReadLine();
返回0;
}

当用户输入大于220的值作为起始位置时,此行

trace[start] =  -1;
将引发异常,因为
start
确实超出了
trace
的范围。因此,您需要强制用户输入您可以处理的内容。像这样:

Console.Write("Please Input the Starting Node : \n");
starting_position = Convert.ToInt32(Console.ReadLine());
while (starting_position < 0 || starting_position >= trace.Count)
{
    Console.Write("Starting Node is invalid. Should be between 0 and 220. Please enter another one  : \n");
    starting_position = Convert.ToInt32(Console.ReadLine());
}
  • 或者您可以使用数组而不是列表:

    int[] trace = new int[220];
    

  • 你如何调用这个函数?
    start
    包含什么值,以及
    trace
    的大小是多少?@mahdad,对不起,我没有意识到问题在别处。请查看更新
    List<int> trace = Enumerable.Repeat(0, 220).ToList();
    
    int[] trace = new int[220];