Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions problems/0200.岛屿数量.广搜版.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,50 @@ public:
};

```

## 其他语言版本

### Java

```java
class Solution {

boolean[][] visited;
int[][] move = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

public int numIslands(char[][] grid) {
int res = 0;
visited = new boolean[grid.length][grid[0].length];
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
if(!visited[i][j] && grid[i][j] == '1') {
bfs(grid, i, j);
res++;
}
}
}
return res;
}

//将这片岛屿上的所有陆地都访问到
public void bfs(char[][] grid, int y, int x) {
Deque<int[]> queue = new ArrayDeque<>();
queue.offer(new int[]{y, x});
visited[y][x] = true;
while(!queue.isEmpty()) {
int[] cur = queue.poll();
int m = cur[0];
int n = cur[1];
for(int i = 0; i < 4; i++) {
int nexty = m + move[i][0];
int nextx = n + move[i][1];
if(nextx < 0 || nexty == grid.length || nexty < 0 || nextx == grid[0].length) continue;
if(!visited[nexty][nextx] && grid[nexty][nextx] == '1') {
queue.offer(new int[]{nexty, nextx});
visited[nexty][nextx] = true; //只要加入队列就标记为访问
}
}
}
}
}
```
77 changes: 76 additions & 1 deletion problems/0695.岛屿的最大面积.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,81 @@ public:

# 其它语言版本

## Python
### BFS
```python
class Solution:
def __init__(self):
self.count = 0

def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
# 与200.独立岛屿不同的是:此题grid列表内是int!!!

# BFS
if not grid: return 0

m, n = len(grid), len(grid[0])
visited = [[False for i in range(n)] for j in range(m)]

result = 0
for i in range(m):
for j in range(n):
if not visited[i][j] and grid[i][j] == 1:
# 每一个新岛屿
self.count = 0
print(f'{self.count}')
self.bfs(grid, visited, i, j)
result = max(result, self.count)

return result

def bfs(self, grid, visited, i, j):
self.count += 1
visited[i][j] = True

queue = collections.deque([(i, j)])
while queue:
x, y = queue.popleft()
for new_x, new_y in [(x + 1, y), (x - 1, y), (x, y - 1), (x, y + 1)]:
if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]) and not visited[new_x][new_y] and grid[new_x][new_y] == 1:
visited[new_x][new_y] = True
self.count += 1
queue.append((new_x, new_y))
```
### DFS
```python
class Solution:
def __init__(self):
self.count = 0

def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
# DFS
if not grid: return 0

m, n = len(grid), len(grid[0])
visited = [[False for _ in range(n)] for _ in range(m)]

result = 0
for i in range(m):
for j in range(n):
if not visited[i][j] and grid[i][j] == 1:
self.count = 0
self.dfs(grid, visited, i, j)
result = max(result, self.count)
return result

def dfs(self, grid, visited, x, y):
if visited[x][y] or grid[x][y] == 0:
return
visited[x][y] = True
self.count += 1
for new_x, new_y in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]):
self.dfs(grid, visited, new_x, new_y)
```



## Java

这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。
Expand Down Expand Up @@ -199,4 +274,4 @@ public int dfs(int[][] grid,int i,int j){
dfs(grid,i,j + 1) +
dfs(grid,i,j - 1);
}
```
```