【问题标题】:Finding the number of areas of true values in a 2D array查找二维数组中真值区域的数量
【发布时间】:2021-04-20 02:43:17
【问题描述】:

我有一个关于我的家庭作业的问题。 所以我需要构建一个程序来计算二维数组中真值区域(彼此接近)的数量。

{0,1,0,0}
{1,0,0,1}
{1,1,0,1}
{1,0,0,1}

所以程序需要返回3,因为3 的地方为真。 如果没有 true 则返回 0。 该程序需要递归且没有循环。 提前感谢那些将帮助我解决这个问题的人。我什至不知道如何开始。

【问题讨论】:

    标签: java arrays recursion multidimensional-array


    【解决方案1】:

    好的,您可以使用dfs 记录您去过的单元格,您可以使用boolean[][] 来记录您去过的单元格。然后,您只需遍历网格以查看您没有去过的地方。如果你还没有去那里,去那里并在附近的单元格上执行dfs,但无论如何,代码会更清晰。

    public static int[][] grid;
    public static boolean[][] went;
    public static int r, c;
    
    public static void dfs(int x, int y) {
        if (x < 0 || x >= r || y < 0 || y >= c) {//out of the grid
            return;
        }
    
        if (went[x][y]) {//I went here
            return;
        }
        went[x][y] = true;
    
        if (grid[x][y] == 0) {//It's false here so don't go
            return;
        }
        dfs(x - 1, y);
        dfs(x + 1, y);
        dfs(x, y - 1);
        dfs(x, y + 1);
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader input =
                new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(input.readLine());
    
        //input reading
        r = Integer.parseInt(st.nextToken());
        c = Integer.parseInt(st.nextToken());
    
        grid = new int[r][c];
        went = new boolean[r][c];
    
        for (int i = 0; i < r; i++) {
            st = new StringTokenizer(input.readLine());
            for (int j = 0; j < c; j++) {
                grid[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        //end of input reading
        int cnt = 0;//number of areas
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                // If I have not went have and the value
                // must be true so I would perform a dfs
                // (since I only care about the value of 1s)
                if (!went[i][j] && grid[i][j] == 1) {
                    cnt++;//add to our counter
                    dfs(i, j);//have not went here
                }
            }
        }
        System.out.println(cnt);
    }
    

    【讨论】:

    • 感谢您的回答,但我需要在没有循环的情况下这样做
    • @MichaelBaazov 所以连一个循环都没有?那么你如何读取输入呢?或者是给你的输入(作为一个变量)?
    【解决方案2】:

    此解决方案使用唯一的递归。没有循环。

    public static void main(String... args) {
        System.out.println(countTrueAreas(new int[][] {
                { 0, 1, 0, 0 },
                { 1, 0, 0, 1 },
                { 1, 1, 0, 1 },
                { 1, 0, 0, 1 } }));  // 3
    }
    
    public static int countTrueAreas(int[][] grid) {
        return countTrueAreas(grid, 0, 0, 10) - 10;
    }
    
    private static int countTrueAreas(int[][] grid, int row, int col, int num) {
        if (row == grid.length)
            return num;
        if (col == grid[row].length) {
            if (++row == grid.length)
                return num;
            col = 0;
        }
    
        if (grid[row][col] == 1)
            dfs(grid, row, col, num++);
    
        return countTrueAreas(grid, row, col + 1, num);
    }
    
    private static void dfs(int[][] grid, int row, int col, int num) {
        if (row < 0 || row >= grid.length)
            return;
        if (col < 0 || col >= grid[row].length)
            return;
        if (grid[row][col] != 1)
            return;
    
        grid[row][col] = num;
        dfs(grid, row, col - 1, num);
        dfs(grid, row, col + 1, num);
        dfs(grid, row - 1, col, num);
        dfs(grid, row + 1, col, num);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 2019-07-21
      • 2013-03-20
      • 2016-05-11
      • 1970-01-01
      • 2021-06-09
      • 2017-04-03
      • 2018-07-05
      • 1970-01-01
      相关资源
      最近更新 更多