递归堆栈溢出错误

发布于 2024-12-14 14:10:36 字数 1577 浏览 1 评论 0原文

好吧,我正在尝试编写一个 Java 程序来解决绘图板问题,但我不断收到 Stackoverflow 错误。我目前只是自学一点Java,所以我喜欢使用我所知道的东西,而不是在网上寻找解决方案,尽管我的方法显然没有那么有效。我能想到解决这个问题的唯一方法是通过一种蛮力,尝试每一种可能性。问题是,我知道这个函数有效,因为它适用于较小尺寸的板,唯一的问题是,对于较大的板,我往往会在函数完成之前出现错误。

所以 char[][] a 只是包含所有 X 和 O 的游戏板。 int[][] b 是一个数组,其中包含为图形十字板分配的数字,例如游戏顶部和左侧的数字。 isDone() 只是检查棋盘是否与给定的数字匹配,而 shift() 向下移动一列。我不想粘贴我的整个程序,所以如果您需要更多信息,请告诉我。谢谢!

由于有人问,我添加了班次代码。 Shift 只是将一行中的所有字符向上移动一个单元格。

更新:我想也许我的代码没有遍历每个组合,因此它跳过了正确的答案。任何人都可以验证这实际上是在尝试所有可能的组合吗?因为这可以解释为什么我会收到 stackoverflow 错误。但另一方面,在太多之前可以进行多少次迭代?

public static void shifter(char[][] a, int[][] b, int[] clockwork)
{
    boolean correct = true;

    correct = isDone(a, b);

    if(correct)
        return;

    clockwork[a[0].length - 1]++;

        for(int x = a[0].length - 1; x > 0; x--)
        {
            if(clockwork[x] > a.length)
            {
                shift(a, x - 1);

                clockwork[x - 1]++;
                clockwork[x] = 1;
            }

            correct = isDone(a, b);

            if(correct)
                return;
        }

    shift(a, a[0].length - 1);

    correct = isDone(a, b);

    if(correct)
        return;

    shifter(a, b, clockwork);

    return;
}

public static char[][] shift(char[][] a, int y)
{       
        char temp = a[0][y];

            for(int shifter = 0; shifter < a.length - 1; shifter++)
            {
                a[shifter][y] = a[shifter + 1][y];
            }

        a[a.length - 1][y] = temp;

    return a;
}

Alright, so I'm trying to make a Java program to solve a picross board, but I keep getting a Stackoverflow error. I'm currently just teaching myself a little Java, and so I like to use the things I know rather than finding a solution online, although my way is obviously not as efficient. The only way I could think of solving this was through a type of brute force, trying every possibility. The thing is, I know that this function works because it works for smaller sized boards, the only problem is that with larger boards, I tend to get errors before the function finishes.

so char[][] a is just the game board with all the X's and O's. int[][] b is an array with the numbers assigned for the picross board like the numbers on the top and to the left of the game. isDone() just checks if the board matches up with the given numbers, and shift() shifts one column down. I didn't want to paste my entire program, so if you need more information, let me know. Thanks!

I added the code for shift since someone asked. Shift just moves all the chars in one row up one cell.

Update: I'm thinking that maybe my code isn't spinning through every combination, and so it skips over the correct answer. Can anyone verify is this is actually trying every possible combination? Because that would explain why I'm getting stackoverflow errors. On the other hand though, how many iterations can this go through before it's too much?

public static void shifter(char[][] a, int[][] b, int[] clockwork)
{
    boolean correct = true;

    correct = isDone(a, b);

    if(correct)
        return;

    clockwork[a[0].length - 1]++;

        for(int x = a[0].length - 1; x > 0; x--)
        {
            if(clockwork[x] > a.length)
            {
                shift(a, x - 1);

                clockwork[x - 1]++;
                clockwork[x] = 1;
            }

            correct = isDone(a, b);

            if(correct)
                return;
        }

    shift(a, a[0].length - 1);

    correct = isDone(a, b);

    if(correct)
        return;

    shifter(a, b, clockwork);

    return;
}

public static char[][] shift(char[][] a, int y)
{       
        char temp = a[0][y];

            for(int shifter = 0; shifter < a.length - 1; shifter++)
            {
                a[shifter][y] = a[shifter + 1][y];
            }

        a[a.length - 1][y] = temp;

    return a;
}

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

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

发布评论

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

评论(1

深空失忆 2024-12-21 14:10:36

检查递归调用并给出终止条件。

if(terminate condition)
{
exit();
}
else
{
call shifter()
}

Check Recursive call.and give the termination condition.

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