我试图计算每一组零的相邻零的数目。我使用了一个修改过的floodfill版本,它返回它所填充的元素的数量。然后我把对floodfill的调用放入一个循环中。我不明白这是怎么回事。
正确输出:
2 4个
电流输出:
2 2 2个

public class Test {
    public static void main(String[] args) {
        String[] in = getInput();
        char[][] map = getMap(Arrays.copyOfRange(in, 0, in.length));
        House h = new House(map);
        System.out.println(h);

    }

    private static String[] getInput() {
        String[] ret = {
                "11111",
                "10011",
                "11101",
                "10001",
                "11111"
            };
        return  ret;
    }

    private static char[][] getMap(String[] copyOfRange) {
        List<char[]> ret = new ArrayList<>();
        for (String x : copyOfRange)
            ret.add(x.toCharArray());
        return  ret.toArray(new char[ret.size()][]);
    }

}

class House {
    private char[][] map;
    List<Integer> listOfAreas;
    House(char[][] map) {
        this.map = map;
        listOfAreas = getAreas();
    }


    private List<Integer> getAreas() {
        List<Integer> ret = new ArrayList<>();
        char[][] cMap = map.clone();
        for (int i = 0; i < cMap.length; i++) {
            for (int j = 0; j < cMap[i].length; j++) {
                if (cMap[i][j] == '0')
                    ret.add(countFlood(new Point(i,j),cMap));
            }
        }
        return ret;
    }

    private int countFlood(Point start, char[][] cMap) {
        int count = 0;
        Stack<Point> stack = new Stack<>();
        stack.push(start);
        while (!stack.isEmpty()) {
            Point p = stack.pop();
            if (cMap[p.getX()][p.getY()] == '0') {
                ++count;
                cMap[p.getX()][p.getY()] = '1';
                for (Point x : getAdj(p,cMap))
                    stack.push(x);

            }
        }
        return count;
    }

    private Point[] getAdj(Point p, char[][] cMap) {
        List<Point> ret = new ArrayList<Point>();
        if (p.getX() == 0)
            ret.add(new Point(p.getX() - 1, p.getY()));
        if (p.getX() != cMap.length - 1)
            ret.add(new Point(p.getX() + 1, p.getY()));
        if (p.getY() == 0)
            ret.add(new Point(p.getX(), p.getY() - 1));
        if (p.getY() != cMap[p.getX()].length - 1)
            ret.add(new Point(p.getX(), p.getY() + 1));

        return ret.toArray(new Point[ret.size()]);
    }


    public String toString() {
        StringBuilder ret = new StringBuilder();
        for (int x : listOfAreas)
            ret.append(x + " ");

        return ret.toString();
    }

}

class Point {
    private int x;
    private int y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

最佳答案

问题出在你的代码中:只有当对应的坐标为零时,你才需要添加向下和向左的点;只有当坐标不为零时,你才应该添加:

private Point[] getAdj(Point p, char[][] cMap) {
    List<Point> ret = new ArrayList<Point>();
    if (p.getX() != 0) // <<== Use != instead of ==
        ret.add(new Point(p.getX() - 1, p.getY()));
    if (p.getX() != cMap.length - 1)
        ret.add(new Point(p.getX() + 1, p.getY()));
    if (p.getY() != 0) // <<== Use != instead of ==
        ret.add(new Point(p.getX(), p.getY() - 1));
    if (p.getY() != cMap[p.getX()].length - 1)
        ret.add(new Point(p.getX(), p.getY() + 1));

    return ret.toArray(new Point[ret.size()]);
}

09-05 19:01