当前位置: 首页 > 知识库问答 >
问题:

Java在多维递归中查找“污点”

宰父深
2023-03-14

我正在尝试解决这个递归练习:

在多维板(M x N)中,每个元素都可以是空的,也可以是满的。

“污点”大小是相邻且值为“x”的元素的数量。

  | 0 | 1 | 2 | 3 | 4 |
0 |   | x |   |   | x |
1 | x |   |   | x | x |
2 |   |   | x | x |   |
3 | x |   |   |   |   |
4 | x | x | x |   |   |
    null
public static int stain (char [][] mat, int row, int col)

共有1个答案

彭涵衍
2023-03-14
    public static int size(boolean[][] a, int i, int j) {
    boolean[][] temp = new boolean[a.length][a[0].length];
    if (exists(a, i, j) && isStained(a, i, j)) {
        temp[i][j] = true;
        if (i == a.length - 1) {
            return size(a, i, j, temp, 1, 0, false, true);
        }
        return size(a, i, j, temp, 1, 0, false, false);
    }
    return 0;
}

private static boolean exists(boolean[][] a, int i, int j) {
    if ((i < 0) || (i >= a.length) || (j < 0) || (j >= a[0].length)) {
        return false;
    }
    return true;
}

private static boolean isStained(boolean[][] a, int i, int j) {
    if (a[i][j] == false) {
        return false;
    }
    return true;
}

private static int size(boolean[][] a, int i, int j, boolean[][] temp, int count, int iLenght, boolean progress,
        boolean dir) {
    if (exists(a, i - 1, j - 1)) {
        if (isStained(a, i - 1, j - 1)) {
            if (temp[i - 1][j - 1] == false) {
                progress = true;
                temp[i - 1][j - 1] = true;
                return size(a, i - 1, j - 1, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (exists(a, i - 1, j)) {
        if (isStained(a, i - 1, j)) {
            if (temp[i - 1][j] == false) {
                progress = true;
                temp[i - 1][j] = true;
                return size(a, i - 1, j, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (exists(a, i - 1, j + 1)) {
        if (isStained(a, i - 1, j + 1)) {
            if (temp[i - 1][j + 1] == false) {
                progress = true;
                temp[i - 1][j + 1] = true;
                return size(a, i - 1, j + 1, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (exists(a, i, j - 1)) {
        if (isStained(a, i, j - 1)) {
            if (temp[i][j - 1] == false) {
                progress = true;
                temp[i][j - 1] = true;
                return size(a, i, j - 1, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (exists(a, i, j + 1)) {
        if (isStained(a, i, j + 1)) {
            if (temp[i][j + 1] == false) {
                progress = true;
                temp[i][j + 1] = true;
                return size(a, i, j + 1, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (exists(a, i + 1, j - 1)) {
        if (isStained(a, i + 1, j - 1)) {
            if (temp[i + 1][j - 1] == false) {
                progress = true;
                temp[i + 1][j - 1] = true;
                return size(a, i + 1, j - 1, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (exists(a, i + 1, j)) {
        if (isStained(a, i + 1, j)) {
            if (temp[i + 1][j] == false) {
                progress = true;
                temp[i + 1][j] = true;
                return size(a, i + 1, j, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (exists(a, i + 1, j + 1)) {
        if (isStained(a, i + 1, j + 1)) {
            if (temp[i + 1][j + 1] == false) {
                progress = true;
                temp[i + 1][j + 1] = true;
                return size(a, i + 1, j + 1, temp, ++count, iLenght, progress, dir);
            }
        }
    }
    if (iLenght >= a.length || progress == false) {
        return count;
    } else {
        ++iLenght;
        if (dir == true) {
            i = i + iLenght;
        } else {
            i = iLenght;
        }
        progress = false;
        return size(a, i, j, temp, count, iLenght, progress, dir);
    }
}
 类似资料:
  • 问题内容: 我不是SQL专家,但是如果有人可以帮助我。 我使用递归CTE来获取如下值。 Child1 –> Parent 1 Parent1 –> Parent 2 Parent2 –> NULL 如果数据填充出错,那么我将遇到以下类似情况,因此CTE可能会进入无限递归循环并给出最大递归错误。由于数据量很大,因此我无法手动检查此 错误数据 。请让我知道是否有办法找到它。 Child1 –> Par

  • 问题内容: 我将以说这是家庭作业为开头。我只是在寻找一些指示。我一直在为此绞尽脑汁,对于我的一生,我只是不明白。我们被要求在列表中找到最小的元素。我知道我在这里需要一个子列表,但是在那之后我不确定。任何指针都很棒。谢谢。 问题答案: 从最一般的意义上讲,递归是一个基于分解工作的概念,然后将较小的工作分派给自己的副本。为了使递归正常工作,您需要三件事: 工作细目。您如何使每个步骤变得“简单”? 递归

  • 我想知道我可以在给定的数组中计算2条特定路径吗。 > < li> 如何返回从[0][0]到[m][n]的最短(或最长)路径?我设法递归地遍历数组,但是我不知道如何“保存”路径并检查哪一个返回的路径更小。 第二个请求是一个我已经纠结了很长时间的问题,但我看到了关于使用和计算这些数组中的值的其他问题。

  • 我试图找到最小元素并删除它,但不幸的是我不能。我想得到一些帮助,这是我的代码。 我在Class Stack中只有这些方法:equals,is空,pop,推送,top,toString(主要方法)。 提前感谢。

  • 我有TreeNode类——非二叉树(

  • 问题内容: 我有一个类似于下面的多维数组。我试图实现的是一种从数组中查找和获取“ Total”值最高的数组的方法,现在我知道有一个称为的函数,但不适用于像这样的多维数组。 我想做的是创建一个foreach循环并仅使用总数构建一个新数组,然后使用它来找到最大值,这将起作用,唯一的问题是检索与此相关的其余数据最大值。我不确定这也是最有效的方法。 有任何想法吗? 问题答案: 从PHP 5.5开始,您可以