695. Max Area of Island(dfs)

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

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.


题目意思是1表示一块地,要我们找出这个由一连在一起的总的地是多大。就是全都相连的1的个数有多少个

这题可以用深搜来做,从0,0开始到最后一个index开始遍历,每个index我们都用dfs来搜一遍

dfs中让该index从上下左右四个方向来搜索,判断跳出递归条件就是index不在范围之内了,或者是这块地我们已经搜过了还有就是这处index根本不是land

    	if (x>=grid.length || x<0 || y>=grid[0].length || y<0 || seen[x][y] || grid[x][y]==0) {
    		return 0;
    	}
public class MaxArea {
	static boolean seen[][];
    public int maxAreaOfIsland(int[][] grid) {
    	seen = new boolean[grid.length][grid[0].length];
    	int ans = 0;
    	for (int i=0; i<grid.length; i++) {
    		for (int j=0; j<grid[0].length; j++) {
    			ans = Math.max(ans, dfs(i, j, grid));
    		}
    	}
    	
    	return ans;
    }
    
    static int dfs(int x, int y, int grid[][]) {
    	int num = 0;
    	if (x>=grid.length || x<0 || y>=grid[0].length || y<0 || seen[x][y] || grid[x][y]==0) {
    		return 0;
    	}
    	
    	if (seen[x][y]==false && grid[x][y]==1) {
        	seen[x][y] = true;
        	num += 1;
        	num += dfs(x-1, y, grid);
        	num += dfs(x+1, y, grid);
        	num += dfs(x, y+1, grid);
        	num += dfs(x, y-1, grid);
    	}
    	
    	return num;
    }
    
    public static void main(String args[]) {
    	int grid[][] = {{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}};
    	System.out.println(new MaxArea().maxAreaOfIsland(grid));
    }
}


查看评论

Windows Server 2008 R2 部署并管理文件服务器视频课程(DFS)

windows server 2008 R2全套课程如下,【全套学习更系统、更全面、更实惠】: Windows Server 2008 R2 系统管理篇全套课程:https://edu.csdn.net/course/detail/4655 Windows Server 2008 R2 系统应用篇全套课程:https://edu.csdn.net/course/detail/7172
  • 2018年03月06日 00:02

leetcode 695(Max Area of Island)

leetcode
  • zlzl8885
  • zlzl8885
  • 2017-10-09 15:57:19
  • 1121

【启发】leetcode - 695. Max Area of Island【回溯法 + 图的遍历 】

题目 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing ...
  • TheSnowBoy_2
  • TheSnowBoy_2
  • 2017-10-08 11:21:51
  • 1796

DFS:695. Max Area of Island

这道题的意思是,给一个表格,由0或者1填充,求相连的1的最大面积。 这道题我开辟了一个和表格一样大的二维数组,用于记录这个点是否已经计算过了。dfs这个函数返回的是,当前点所在的一片1的面积。 ...
  • u012528000
  • u012528000
  • 2017-10-13 23:21:13
  • 148

Max Area of Island问题及解法

695. Max Area of Island
  • u011809767
  • u011809767
  • 2017-10-09 09:07:07
  • 1526

695. Max Area of Island (Swift)

题目地址:https://leetcode.com/problems/max-area-of-island/description/ 题目(岛的最大区域) Given a non-empt...
  • C_calary
  • C_calary
  • 2018-01-27 13:29:38
  • 56

695. Max Area of Island

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) co...
  • SHURamos
  • SHURamos
  • 2017-10-09 15:15:33
  • 366

leetcode 695 Max Area of Island

class Solution { public int maxAreaOfIsland(int[][] grid) { int max_air=0; ...
  • MRxjh
  • MRxjh
  • 2017-10-11 11:20:49
  • 196

LeetCode695:Max Area of Island 解答

写这道题用了我一个小时的时间,算是比较难的一道题目,解题的过程也很干净利落下面来看一下题目 Given a non-empty 2D array grid of 0’s and 1’s, an i...
  • neverever01
  • neverever01
  • 2017-10-16 15:59:38
  • 372

Leetcode#695. Max Area of Island(岛屿最大面积,深搜dfs)

题目 Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) ...
  • xunalove
  • xunalove
  • 2018-02-19 16:15:45
  • 107
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 683
    排名: 7万+
    文章分类
    最新评论