关闭

LWC 53:695. Max Area of Island

692人阅读 评论(0) 收藏 举报
分类:

LWC 53:695. Max Area of Island

传送门:695. Max Area of Island

Problem:

Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (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 1:

[[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]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

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

Given the above grid, return 0.

Note:

The length of each dimension in the given grid does not exceed 50.

思路:
DFS求出邻接的面积,更新每个区域的最大值即可。

代码如下:

    int[][] dir = {{1, 0},{-1, 0},{0, 1},{0, -1}};
    public int maxAreaOfIsland(int[][] grid) {
        int n = grid.length;
        if (n == 0) return 0;
        int m = grid[0].length;
        if (m == 0) return 0;

        int max = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == 1) {
                    max = Math.max(max, dfs(grid, i, j, n, m, new boolean[n][m]));
                }
            }
        }

        return max;
    }

    public int dfs(int[][] grid, int i, int j, int n, int m, boolean[][] vis) {
        int res = 1;
        vis[i][j] = true;
        for (int[] d : dir) {
            int nx = i + d[0];
            int ny = j + d[1];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && grid[nx][ny] == 1) {
                res += dfs(grid, nx, ny, n, m, vis);
            }
        }
        return res;
    }
1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Jungle Roads(最小生成树)

题目 The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid mon...
  • zinss26914
  • zinss26914
  • 2013-04-13 19:03
  • 1524

libsvm代码阅读:关于Solver类分析(二)

如果你看完了上篇博文的伪代码,那么我们就可以开始谈谈它的源代码了。 // An SMO algorithm in Fan et al., JMLR 6(2005), p. 1889--1918 // ...
  • Linoi
  • Linoi
  • 2014-02-22 22:15
  • 3296

Android Studio更新升级方法

自从2013 Google I/O大会之后,笔者就将android ide开发工具从eclipse迁移到Android Studio了,android studio一直在更新完善,为了与时俱进,我们当...
  • zgxzgxzg
  • zgxzgxzg
  • 2015-09-15 11:06
  • 2541

Graph problem of Island of shortest road

  • 2012-11-12 16:52
  • 3KB
  • 下载

LIGHTOJ 1265 - Island of Survival [递推|概率dp]【杂类|动态规划】

题目连接:http://lightoj.com/volume_showproblem.php?problem=1265 ——————————————————————————–. 1265 - Is...
  • qq_33184171
  • qq_33184171
  • 2017-01-12 15:48
  • 238

LightOJ1265---Island of Survival (概率dp)

You are in a reality show, and the show is way too real that they threw into an island. Only two kin...
  • Guard_Mine
  • Guard_Mine
  • 2015-05-05 16:42
  • 1084

[暴力搜索]Island of Logic UVA592

暴力搜索的一道题目。
  • u011194165
  • u011194165
  • 2013-12-18 15:03
  • 788

LightOJ 1265 Island of Survival(概率DP)

1265 - Island of Survival You are in a reality show, and the show is way too real that they threw...
  • qq_26658823
  • qq_26658823
  • 2016-08-09 15:02
  • 218

HDU 2416 POJ 3346 Treasure of the Chimp Island(最短路)

题意:问能否从城外进入城堡,并取得宝藏。求取得宝藏所需的最短时间。。 1,边界有字母,或者#表示入口。#表示从这里进入可以获得0个炸弹,A表示可以获得1个炸弹,B两个。。。 2, 星号表示障碍,不...
  • binwin20
  • binwin20
  • 2012-08-13 11:28
  • 938

Treasure of the Chimp Island (BFS

Treasure of the Chimp Island Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
  • zzcblogs
  • zzcblogs
  • 2017-04-21 20:01
  • 158
    个人资料
    • 访问:184857次
    • 积分:5057
    • 等级:
    • 排名:第6428名
    • 原创:318篇
    • 转载:1篇
    • 译文:0篇
    • 评论:45条
    博客专栏
    最新评论