使用floodfill计算矩阵中的adjecent零点

时间:2014-04-27 12:49:25

标签: java algorithm matrix flood-fill

我试图计算每组零的adjecent零数。我使用了洪水填充的修改版本,它返回了它填充的元素数量。然后我将调用洪水填充到循环中。我不明白它的错误。

正确输出: 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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

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()]);
}