victory的博客

长安一片月,万户捣衣声

0%

leetcode | 695.岛屿的最大面积

695.岛屿的最大面积

题目描述

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

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

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

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

示例 1:
输入: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,0]]
输出:0

题目链接

思路

  1. 深度优先搜索(DFS)
    利用深度优先搜索(DFS)对每个岛屿进行遍历,找出最大值
    (1)从每个陆地出发,遍历该陆地所在的岛屿
    (2)在遍历某一岛屿的时候:
    1)从隶属于该岛屿的某一块陆地出发,向四个方向递归地DFS
    2)每次递归对下标进行判断,以区域的边界作为递归边界
    3)为保证每块陆地只访问一次,将已访问过的陆地置0
    4)递归地返回整块岛屿的面积
    (3)找出所有岛屿的最大值,即为答案

邻接矩阵和邻接表表示的图的DFS和BFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package test;

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

class MGraph{
private static final int MAXVEX = 100;
private static final int INFINITY = 65535;

int[] vexs = new int[MAXVEX];
int[][] arc = new int[MAXVEX][MAXVEX];
int numVertexes;
int numEdges;
boolean[] visited = new boolean[MAXVEX];

public void createMGraph() {
System.out.println("输入顶点数和边数:\n");
Scanner scanner = new Scanner(System.in);
numVertexes = scanner.nextInt();
numEdges = scanner.nextInt();

for(int i = 0; i < numVertexes; i++) {
vexs[i] = scanner.nextInt();
}

for(int i = 0; i < numVertexes; i++) {
for(int j = 0; j < numVertexes; j++) {
arc[i][j] = INFINITY;
}
}

for(int k = 0; k < numEdges; k++) {
System.out.println("输入边(vi,vj)上的下标i,j和权w:\n");
int i = scanner.nextInt();
int j = scanner.nextInt();
int w = scanner.nextInt();
arc[i][j] = w;
arc[j][i] = arc[i][j];
}
}

public void DFS(int i) {
visited[i] = true;
System.out.println(vexs[i] + " ");
for(int j = 0; j < numVertexes; j++) {
if(arc[i][j] == 1 && !visited[j]) {
DFS(j);
}
}
}

public void DFSTraverse() {
for(int i = 0; i < numVertexes; i++) {
visited[i] = false;
}

for(int i = 0; i < numVertexes; i++) {
if(!visited[i]) {
DFS(i);
}
}
}

public void BFSTraverse() {
LinkedList<Integer> queue = new LinkedList<Integer>();

for(int i = 0; i < this.numVertexes; i++) {
this.visited[i] = false;
}

for(int i = 0; i < this.numVertexes; i++) {//对每一个顶点做循环
if(!visited[i]) {//若是未访问过就处理
visited[i] = true;//设置当前顶点访问过
System.out.println(this.vexs[i]);//打印顶点,也可以其他操作
queue.addLast(i);//将此顶点入队列
while(!queue.isEmpty()) {
queue.pop();
for(int j = 0; j < this.numVertexes; j++) {
if(this.arc[i][j] == 1 && !this.visited[j]) {
visited[j] = true;
System.out.println(this.vexs[j]);
queue.addLast(j);
}
}
}
}
}
}
}

class EdgeNode{
int adjvex;
EdgeNode next;
}

class VertexNode{
int data;
EdgeNode firstedge;
}

class GraphAdjList{
private static final int MAXVEX = 100;
VertexNode[] adjList= new VertexNode[MAXVEX];
int numVertexes;
int numEdges;
boolean[] visited = new boolean[MAXVEX];

public void createALGraph() {
Scanner scanner = new Scanner(System.in);
System.out.println("输入顶点数和边数:");
this.numVertexes = scanner.nextInt();
this.numEdges = scanner.nextInt();
for(int i = 0 ; i < this.numVertexes; i++) {
VertexNode node = new VertexNode();
node.data = scanner.nextInt();
node.firstedge = null;
this.adjList[i] = node;
}
for(int k = 0; k < this.numEdges; k++) {
System.out.println("输入边(vi,vj)上的顶点序号:\n");
int i = scanner.nextInt();
int j = scanner.nextInt();
EdgeNode e = new EdgeNode();
e.adjvex = j;
e.next = this.adjList[i].firstedge;
this.adjList[i].firstedge = e;

EdgeNode e2 = new EdgeNode();
e2.adjvex = i;
e2.next = this.adjList[j].firstedge;
this.adjList[j].firstedge = e2;
}
}

public void DFS(int i) {
visited[i] = true;
System.out.println(this.adjList[i].data + " ");
EdgeNode p = new EdgeNode();
p = this.adjList[i].firstedge;
while(p != null) {
if(!visited[p.adjvex]) {
DFS(p.adjvex);
}
p = p.next;
}
}

public void DFSTraverse() {
for(int i = 0; i < this.numVertexes; i++) {
this.visited[i] = false;
}

for(int i = 0; i < this.numVertexes; i++) {
if(!visited[i]) {
this.DFS(i);
}
}
}

public void BFSTraverse() {
LinkedList<Integer> queue = new LinkedList<Integer>();
EdgeNode p = new EdgeNode();
for(int i = 0; i < this.numVertexes; i++) {
this.visited[i] = false;
}
for(int i = 0; i < this.numVertexes; i++) {
if(!this.visited[i]) {
this.visited[i] = true;
System.out.println(this.adjList[i].data);//打印顶点
queue.addLast(i);
while(!queue.isEmpty()) {
queue.pop();
p = this.adjList[i].firstedge;
while(p != null) {
if(!this.visited[p.adjvex]) {
this.visited[p.adjvex] = true;
System.out.println(this.adjList[p.adjvex].data);
queue.add(p.adjvex);
}
p = p.next;
}
}
}

}
}
}

public class DFSAndBFS {
public static void main(String[] args) {
//邻接矩阵
MGraph graph = new MGraph();
graph.createMGraph();
System.out.println("邻接矩阵:\n");
for(int i = 0; i < graph.numVertexes; i++) {
for(int j = 0; j < graph.numVertexes; j++) {
System.out.print(graph.arc[i][j]+"\t");
}
System.out.println();
}
System.out.println("深度优先搜索遍历图(邻接矩阵):\n");
graph.DFSTraverse();
System.out.println("广度优先搜索遍历图(邻接矩阵):\n");
graph.BFSTraverse();

//邻接表
GraphAdjList GL = new GraphAdjList();
GL.createALGraph();
System.out.println("深度优先搜索遍历图(邻接表):\n");
GL.DFSTraverse();
System.out.println("广度优先搜索遍历图(邻接表):\n");
GL.BFSTraverse();
}
}

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def island_dfs(self, grid, i, j):
if len(grid) > i >= 0 and len(grid[0]) > j >= 0:
if grid[i][j] == 0:
return 0
else:
grid[i][j] = 0
return 1 + self.island_dfs(grid, i - 1, j) + self.island_dfs(grid, i + 1, j) + \
self.island_dfs(grid, i, j - 1) + self.island_dfs(grid, i, j + 1)
else:
return 0

def max_area_of_island(self, grid):
ans = 0

for i in range(len(grid)):
for j in range(len(grid[0])):
ans = max(ans, self.island_dfs(grid, i, j))

return ans


if __name__ == "__main__":
s = Solution()
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]]
max_area = s.max_area_of_island(grid)
print(max_area)