在某区域中,找出连续空闲区域的最大范围

题目描述

看电影选座位,座位用m*n的二维矩阵表示,用1代表该作为已选,0代表未选。求选择出最大的相邻空座位个数(上下左右为相邻)。

示例一:

  • 输入:
4
7
1 0 0 1 0 0 0
1 0 0 0 0 1 1
0 0 0 1 0 0 0
1 1 0 1 1 0 0
  • 输出:
18

示例二:

  • 输入:
4
7
1 0 0 1 0 0 0
1 0 0 1 0 1 1
0 0 0 1 0 0 0
1 1 0 1 1 0 0
  • 输出:
9

解题思路

1. 递归

查找所有未访问过的坐标点,从某一点开始,递归遍历某点的上下左右,直到所有坐标点都访问过。

class Solution {
    public int maxContinuousVacantSeatsNum(int[][] nums, int m, int n) {
        if (m <= 0 || n <= 0) {
            return 0;
        }
        boolean[][] visited = new boolean[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (nums[i][j] == 1) {
                    visited[i][j] = true;
                }
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                // 访问过的坐标点直接跳过
                if (visited[i][j]) {
                    continue;
                }
                int res = dfs(nums, m, n, i, j, visited);
                ans = Math.max(ans, res);
            }
        }
        return ans;
    }

    public int dfs(int[][] nums, int m, int n, int startX, int startY, boolean[][] visited) {
        if (startX >= m || startY >= n || startX < 0 || startY < 0 || visited[startX][startY]) {
            return 0;
        }
        visited[startX][startY] = true;
        return 1 + dfs(nums, m, n, startX + 1, startY, visited) + dfs(nums, m, n, startX - 1, startY, visited) + dfs(nums, m, n, startX, startY + 1, visited) + dfs(nums, m, n, startX, startY - 1, visited);
    }
}

2. BFS

使用队列记录压缩的二维坐标,这里注意矩阵坐标的对称性,要分为上、下三角矩阵来存储坐标信息。

class Solution {
    public int maxContinuousVacantSeatsNum(int[][] nums, int m, int n) {
        if (m <= 0 || n <= 0) {
            return 0;
        }
        boolean[][] visited = new boolean[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (nums[i][j] == 1) {
                    visited[i][j] = true;
                }
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (visited[i][j]) {
                    continue;
                }
                int res = bfs(nums, m, n, i, j, visited);
                ans = Math.max(ans, res);
            }
        }
        return ans;
    }

    public int bfs(int[][] nums, int m, int n, int startX, int startY, boolean[][] visited) {
        // 主对角线下三角矩阵队列
        Queue<Integer> queue1 = new LinkedList<>();
        // 主对角线上三角矩阵队列
        Queue<Integer> queue2 = new LinkedList<>();
        int count = 0;
        if (startX >= startY) {
            queue1.offer(startX * n + startY);
        } else {
            queue2.offer(startX * n + startY);
        }
        while (!queue1.isEmpty() || !queue2.isEmpty()) {
            int dot = !queue1.isEmpty() ? queue1.poll() : queue2.poll();
            // 上三角矩阵和下三角矩阵,关于主对角线对称的部分,其压缩计算后的值相同,因此需要根据row和col的大小分别计算存取
            int row = dot / n, col = dot % n;
            if (row >= m || col >= n) {
                continue;
            }
            if (!visited[row][col]) {
                visited[row][col] = true;
                if (row >= col) {
                    queue1.offer(row * n + col);
                } else {
                    queue2.offer(row * n + col);
                }
                count++;
            }
            if (col < n - 1 && !visited[row][col + 1]) {
                visited[row][col + 1] = true;
                if (row >= col + 1) {
                    queue1.offer(row * n + col + 1);
                } else {
                    queue2.offer(row * n + col + 1);
                }
                count++;
            }
            if (col > 0 && !visited[row][col - 1]) {
                visited[row][col - 1] = true;
                if (row >= col - 1) {
                    queue1.offer(row * n + col - 1);
                } else {
                    queue2.offer(row * n + col - 1);
                }
                count++;
            }
            if (row < m - 1 && !visited[row + 1][col]) {
                visited[row + 1][col] = true;
                if (row + 1 >= col) {
                    queue1.offer((row + 1) * n + col);
                } else {
                    queue2.offer((row + 1) * n + col);
                }
                count++;
            }
            if (row > 0 && !visited[row - 1][col]) {
                visited[row - 1][col] = true;
                if (row - 1 >= col) {
                    queue1.offer((row - 1) * n + col);
                } else {
                    queue2.offer((row - 1) * n + col);
                }
                count++;
            }
        }
        return count;
    }
}

全部代码

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 输入次数
        int k = scanner.nextInt();
        for (int c = 0; c < k; c++) {
            int m = scanner.nextInt(), n = scanner.nextInt();
            int[][] nums = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    nums[i][j] = scanner.nextInt();
                }
            }
            Main main = new Main();
            System.out.println(main.maxContinuousVacantSeatsNum(nums, m, n));
        }
    }

    private int ans = Integer.MIN_VALUE;

    public int maxContinuousVacantSeatsNum(int[][] nums, int m, int n) {
        if (m <= 0 || n <= 0) {
            return 0;
        }
        boolean[][] visited = new boolean[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (nums[i][j] == 1) {
                    visited[i][j] = true;
                }
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (visited[i][j]) {
                    continue;
                }
                int res = dfs(nums, m, n, i, j, visited);
                ans = Math.max(ans, res);
            }
        }
        return ans;
    }

    public int dfs(int[][] nums, int m, int n, int startX, int startY, boolean[][] visited) {
        if (startX >= m || startY >= n || startX < 0 || startY < 0 || visited[startX][startY]) {
            return 0;
        }
        visited[startX][startY] = true;
        return 1 + dfs(nums, m, n, startX + 1, startY, visited) + dfs(nums, m, n, startX - 1, startY, visited) + dfs(nums, m, n, startX, startY + 1, visited) + dfs(nums, m, n, startX, startY - 1, visited);
    }

    public int bfs(int[][] nums, int m, int n, int startX, int startY, boolean[][] visited) {
        // 主对角线下三角矩阵队列
        Queue<Integer> queue1 = new LinkedList<>();
        // 主对角线上三角矩阵队列
        Queue<Integer> queue2 = new LinkedList<>();
        int count = 0;
        if (startX >= startY) {
            queue1.offer(startX * n + startY);
        } else {
            queue2.offer(startX * n + startY);
        }
        while (!queue1.isEmpty() || !queue2.isEmpty()) {
            int dot = !queue1.isEmpty() ? queue1.poll() : queue2.poll();
            // 上三角矩阵和下三角矩阵,关于主对角线对称的部分,其压缩计算后的值相同,因此需要根据row和col的大小分别计算存取
            int row = dot / n, col = dot % n;
            if (row >= m || col >= n) {
                continue;
            }
            if (!visited[row][col]) {
                visited[row][col] = true;
                if (row >= col) {
                    queue1.offer(row * n + col);
                } else {
                    queue2.offer(row * n + col);
                }
                count++;
            }
            if (col < n - 1 && !visited[row][col + 1]) {
                visited[row][col + 1] = true;
                if (row >= col + 1) {
                    queue1.offer(row * n + col + 1);
                } else {
                    queue2.offer(row * n + col + 1);
                }
                count++;
            }
            if (col > 0 && !visited[row][col - 1]) {
                visited[row][col - 1] = true;
                if (row >= col - 1) {
                    queue1.offer(row * n + col - 1);
                } else {
                    queue2.offer(row * n + col - 1);
                }
                count++;
            }
            if (row < m - 1 && !visited[row + 1][col]) {
                visited[row + 1][col] = true;
                if (row + 1 >= col) {
                    queue1.offer((row + 1) * n + col);
                } else {
                    queue2.offer((row + 1) * n + col);
                }
                count++;
            }
            if (row > 0 && !visited[row - 1][col]) {
                visited[row - 1][col] = true;
                if (row - 1 >= col) {
                    queue1.offer((row - 1) * n + col);
                } else {
                    queue2.offer((row - 1) * n + col);
                }
                count++;
            }
        }
        return count;
    }
}

类似题目

剑指 Offer II 105. 岛屿的最大面积
给定一个由 0 和 1 组成的非空二维数组 grid ,用来表示海洋岛屿地图。

一个 岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在水平或者竖直方向上相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。

找到给定的二维数组中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。

class Solution {
    private int ans = 0;

    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0)    continue;
                ans = Math.max(ans, dfs(grid, i, j, m, n));
            }
        }
        return ans;
    }

    public int dfs(int[][] grid, int startX, int startY, int m, int n) {
        if (startX < 0 || startY < 0 || startX >= m || startY >= n || grid[startX][startY] == 0) {
            return 0;
        }
        grid[startX][startY] = 0;
        return 1 + dfs(grid, startX + 1, startY, m, n) + dfs(grid, startX - 1, startY, m, n) + dfs(grid, startX, startY + 1, m, n) + dfs(grid, startX, startY - 1, m, n);
    }
}
posted @ 2022-04-26 15:07  0202zc  阅读(103)  评论(0编辑  收藏  举报