方形拼图 C#

发布于 2024-11-09 20:00:48 字数 2609 浏览 11 评论 0原文

你能帮助我并纠正我的代码吗

    static void SolveAndDraw(int number)
    {

        // Create Dynamic List of list to 

        List<List<int>> matrix = new List<List<int>>();

        // Intialize the inner lists 
        for (int j = 0; j < number; j++)
        {
            matrix.Add(new List<int>());
        
        
        }

        char direction = 'r';
        int xPos = 0, yPos = 0;
        int rightLimit = number - 1;
        int leftLimit = 0;
        int upLimit = 0;
        int bottomLimit = number - 1;

        for (int i = 1; i <= number * number; ++i)
        {

           // matrix[yPos][xPos] = i;

            matrix[xPos].Insert(yPos, i);
            switch (direction)
            {
                case 'r':
                    if (xPos < rightLimit)
                    {
                        ++xPos;
                    }
                    else
                    {
                        direction = 'd';
                        ++upLimit;
                        ++yPos;
                    }
                    break;


                case 'l':
                    if (xPos > leftLimit)
                    {
                        --xPos;
                    }
                    else
                    {
                        direction = 'u';
                        --bottomLimit;
                        --yPos;
                    }
                    break;

                case 'u':
                    if (yPos > upLimit)
                    {
                        --yPos;
                    }
                    else
                    {
                        direction = 'r';
                        ++leftLimit;
                        ++xPos;
                    }
                    break;

                case 'd':
                    if (yPos < bottomLimit)
                    {
                        ++yPos;
                    }
                    else
                    {
                        direction = 'l';
                        --rightLimit;
                        --xPos;
                    }
                    break;
            }
        }

        // Now, just dump the matrix contents to stdout
        for (int i = 0; i < number; ++i)
        {
            for (int j = 0; j < number; ++j)
            {
                Console.Write("{0}\t", matrix[i][j]);
            }
            Console.Write("\n");

        }

        Console.ReadLine();

    }

它崩溃并给出错误:

索引必须在列表的范围内。

参数名称:索引

Could u help me and correct my code

    static void SolveAndDraw(int number)
    {

        // Create Dynamic List of list to 

        List<List<int>> matrix = new List<List<int>>();

        // Intialize the inner lists 
        for (int j = 0; j < number; j++)
        {
            matrix.Add(new List<int>());
        
        
        }

        char direction = 'r';
        int xPos = 0, yPos = 0;
        int rightLimit = number - 1;
        int leftLimit = 0;
        int upLimit = 0;
        int bottomLimit = number - 1;

        for (int i = 1; i <= number * number; ++i)
        {

           // matrix[yPos][xPos] = i;

            matrix[xPos].Insert(yPos, i);
            switch (direction)
            {
                case 'r':
                    if (xPos < rightLimit)
                    {
                        ++xPos;
                    }
                    else
                    {
                        direction = 'd';
                        ++upLimit;
                        ++yPos;
                    }
                    break;


                case 'l':
                    if (xPos > leftLimit)
                    {
                        --xPos;
                    }
                    else
                    {
                        direction = 'u';
                        --bottomLimit;
                        --yPos;
                    }
                    break;

                case 'u':
                    if (yPos > upLimit)
                    {
                        --yPos;
                    }
                    else
                    {
                        direction = 'r';
                        ++leftLimit;
                        ++xPos;
                    }
                    break;

                case 'd':
                    if (yPos < bottomLimit)
                    {
                        ++yPos;
                    }
                    else
                    {
                        direction = 'l';
                        --rightLimit;
                        --xPos;
                    }
                    break;
            }
        }

        // Now, just dump the matrix contents to stdout
        for (int i = 0; i < number; ++i)
        {
            for (int j = 0; j < number; ++j)
            {
                Console.Write("{0}\t", matrix[i][j]);
            }
            Console.Write("\n");

        }

        Console.ReadLine();

    }

It crashes and gives the error:

Index must be within the bounds of the List.

Parameter name: index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌无敌 2024-11-16 20:00:48

是否需要该列表列表,或者您可以使用二维数组(您通过数字传递维度,为什么需要列表?)

static void SolveAndDraw(int number)
        {

            // Create array with two dimensions of size number
            int[,] matrix = new int[number,number];            

            char direction = 'r';
            int xPos = 0, yPos = 0;
            int rightLimit = number - 1;
            int leftLimit = 0;
            int upLimit = 0;
            int bottomLimit = number - 1;

            for (int i = 1; i <= number * number; ++i)
            {
                matrix[xPos,yPos] = i;
                switch (direction)
                {
                    case 'r':
                        if (xPos < rightLimit)
                        {
                            ++xPos;
                        }
                        else
                        {
                            direction = 'd';
                            ++upLimit;
                            ++yPos;
                        }
                        break;


                    case 'l':
                        if (xPos > leftLimit)
                        {
                            --xPos;
                        }
                        else
                        {
                            direction = 'u';
                            --bottomLimit;
                            --yPos;
                        }
                        break;

                    case 'u':
                        if (yPos > upLimit)
                        {
                            --yPos;
                        }
                        else
                        {
                            direction = 'r';
                            ++leftLimit;
                            ++xPos;
                        }
                        break;

                    case 'd':
                        if (yPos < bottomLimit)
                        {
                            ++yPos;
                        }
                        else
                        {
                            direction = 'l';
                            --rightLimit;
                            --xPos;
                        }
                        break;
                }
            }

            // Now, just dump the matrix contents to stdout
            for (int i = 0; i < number; ++i)
            {
                for (int j = 0; j < number; ++j)
                {
                    Console.Write("{0}\t", matrix[i,j]);
                }
                Console.Write("\n");

            }

            Console.ReadLine();

        }

    }

Is that list of lists required, or can you use an array with 2 dimensions (you are passing the dimension through number, why do you need the lists?)

static void SolveAndDraw(int number)
        {

            // Create array with two dimensions of size number
            int[,] matrix = new int[number,number];            

            char direction = 'r';
            int xPos = 0, yPos = 0;
            int rightLimit = number - 1;
            int leftLimit = 0;
            int upLimit = 0;
            int bottomLimit = number - 1;

            for (int i = 1; i <= number * number; ++i)
            {
                matrix[xPos,yPos] = i;
                switch (direction)
                {
                    case 'r':
                        if (xPos < rightLimit)
                        {
                            ++xPos;
                        }
                        else
                        {
                            direction = 'd';
                            ++upLimit;
                            ++yPos;
                        }
                        break;


                    case 'l':
                        if (xPos > leftLimit)
                        {
                            --xPos;
                        }
                        else
                        {
                            direction = 'u';
                            --bottomLimit;
                            --yPos;
                        }
                        break;

                    case 'u':
                        if (yPos > upLimit)
                        {
                            --yPos;
                        }
                        else
                        {
                            direction = 'r';
                            ++leftLimit;
                            ++xPos;
                        }
                        break;

                    case 'd':
                        if (yPos < bottomLimit)
                        {
                            ++yPos;
                        }
                        else
                        {
                            direction = 'l';
                            --rightLimit;
                            --xPos;
                        }
                        break;
                }
            }

            // Now, just dump the matrix contents to stdout
            for (int i = 0; i < number; ++i)
            {
                for (int j = 0; j < number; ++j)
                {
                    Console.Write("{0}\t", matrix[i,j]);
                }
                Console.Write("\n");

            }

            Console.ReadLine();

        }

    }
生死何惧 2024-11-16 20:00:48

如果您尝试在大于列表当前计数的索引处插入(),则会引发该异常。最有可能的是,您正在以这样的方式遍历“矩阵”,即语句

matrix[xPos].Insert(yPos, i);

插入到大小尚未达到 yPos 的列表中。避免这种情况的最简单方法是首先向每个内部列表添加足够的元素:

    // Intialize the inner lists 
    for (int j = 0; j < number; j++)
    {
        matrix.Add(new List<int>());

        // New code here:
        for (int k = 0; k < number; k++)
            matrix[j].Add(0);
    }

That exception is thrown if you try to Insert() at an index that is larger than the current Count of the list. Most likely you are traversing the "matrix" in such a way that the statement

matrix[xPos].Insert(yPos, i);

is inserting into a list that is not yet of size yPos. The easiest way to avoid this is to add enough elements to each inner list initially:

    // Intialize the inner lists 
    for (int j = 0; j < number; j++)
    {
        matrix.Add(new List<int>());

        // New code here:
        for (int k = 0; k < number; k++)
            matrix[j].Add(0);
    }
泪痕残 2024-11-16 20:00:48

如果没有在我的脑海中运行代码,这一行

for (int i = 1; i <= number * number; ++i)

看起来很可疑,也许你应该从 0 开始,但话又说回来,我可能完全关闭了。

Without running the code in my mind, this line

for (int i = 1; i <= number * number; ++i)

looks suspicious, may be you should start with 0, but then again, I might be totally off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文