LeetCode算法题——Max Area of Island

来源:互联网 发布:如何查询淘宝购买记录 编辑:程序博客网 时间:2024/06/05 21:14

题目概述

Given a non-empty 2D array grid of 0 and 1, an island is a group of 1 (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]

Result: 6

分析

此题考查了图论知识。由于题干中存在着强烈的暗示元素,即“只有与‘1’四个方向相接才算连续的岛屿”,因此最直接想法就是利用BFS,利用其遍历四个方向的特点解决问题。

要求出最大的连续岛屿面积,首先要找到“1”作为一个起点开始进行BFS,如果BFS搜索到新的1,则面积加一;在完成一片岛屿的搜索之后,实时更新当前的最大岛屿面积值。BFS的总体思路不需赘述,此处强调几个值得注意的点:

  • 地图上的”1“分布不确定,因此如果盲目地重复遍历全图,对每个”1“都进行BFS,将极大浪费时间,因此需要实时更新地图,即及时标记已经遍历过的点,确保不重复搜索

  • 方向向量的选择上,使用一维数组也可以模拟出二维的”上下左右“过程,一个参考的向量是{-1, 0, 1, 0, -1},使用时只需为挑选两个连续元素,分别施加在横坐标和纵坐标上

对一个点进行bfs的时候,注意起始面积应设定为1而不是0,后者是惯性思维导致的

以下是完整代码

int maxAreaOfIsland(vector<vector<int>>& grid) {        int res = 0;        int row = grid.size(), col = grid[0].size();        for (int i = 0; i < row; i++)             for (int j = 0; j < col; j++)                if (grid[i][j] == 1) res = max(res, bfs(grid, row, col, i, j));        return res;    }    int bfs(vector<vector<int>>& grid, int row, int col, int x, int y) {        int area = 1;        queue < pair<int, int> > q;        q.push( {x, y} ); // 已经遍历过的点加入队列        grid[x][y] = -1; // 修改地图的值        vector<int> dir = {-1, 0, 1, 0, -1};        while (!q.empty()) {            int temp_r = q.front().first, temp_c = q.front().second; // 获取当前节点的坐标            q.pop();            for (int i = 0; i < 4; i++) {                int r = temp_r + dir[i], c = temp_c + dir[i+1]; // 方向:上右下左                if (r >= 0 && r < row && c >= 0 && c < col && grid[r][c] == 1) {                    area++;                    grid[r][c] = -1; // 更新地图                    q.push( {r, c} ); // 加入新的连续岛屿                }            }        }        return area;    } 

时间复杂度和空间复杂度均为O(M*N),需要注意的是运行时间与地图的稠密程度有很大关系。

总结

  • BFS的使用
  • 实时更新提高效率