• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

695. 岛屿的最大面积

给你一个大小为m x n的二进制矩阵网格。

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

岛屿的面积是岛上值为一的单元格的数目。

计算并返回格子中最大的岛屿面积。如果没有岛屿,则返回面积为0。

示例1:

tx0difn1gfm3585.jpg

输入:grid=[[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]]

输出:6

解释:答案不应该是11 ,因为岛屿只能包含水平或垂直这四个方向上的1 。

示例2:

输入:grid=[[0,0,0,0,0,0]]

输出:0

提示:

m==网格长度

n==grid.长度

1=m,n=50

网格[j]为0或一

一类解决方案{

2 public:

3 static const expr int g _ dire citon[4][2]={ {-1,0},{0,1},{1,0},{0,-1 } };//上、左、下、右四个方向

4个无效BF(矢量向量图,int x,int y,vectorvectorbool visited,int cnt) {

5 int row=graph。size();

6 int col=graph[0].size();

7 queuestd:pairint,int q;//存储坐标位置

8 q.push(make_pair(x,y));

9 visited[x][y]=true;

10 CNT=1;

11 while(!q.empty()) {

12 int size=q . size();

13 for(int I=0;我尺寸;i ) {

14 std:pairint,int position=q . front();

15 q . pop();

16 //向四个方向遍历,坐标有效、值为1(陆地)且未被访问过的坐标入队

17 for(int j=0;j 4;j ) {

18 int xNext=position。第一个g _ direction[j][0];

19 int y next=位置。第二个g _ direction[j][1];

20 if((x next=0x next row)(y next=0y next col))

21 graph[xNext][yNext]==1!已访问[xNext][yNext]) {

22 q.push(make_pair(xNext,yNext));

23已访问[x下一个][y下一个]=true;

24 cnt

25 }

26 }

27 }

28 }

29 }

30 int maxAreaOfIsland(矢量矢量网格){

31 if (grid.size()==0 || grid[0].size()==0) {

32返回0;

33 }

34 int row=grid。size();

35 int col=grid[0].size();

访问了36 vectorvectorbool,vectorbool(col,false));

37 int maxNum=0;

38 for(int I=0;我划船;i ) {

39 for(int j=0;j栏){

40 //非陆地则跳过

41 if (grid[j]==0) {

42继续;

43 }

44 //访问过则跳过

45 if (visited[j]) {

46继续;

47 }

48 int CNT=0;

49个bfs(grid、I、j、visited、CNT);

50 maxNum=max(maxNum,CNT);

51 }

52 }

53返回最高上限人数

54 }

55 };

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now