Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad0d129

Browse files
committed
更新并补充了kama0099.岛屿的数量dfs和bfs的版本,附有详细注释
1 parent fb6dc86 commit ad0d129

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

‎problems/kamacoder/0099.岛屿的数量广搜.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,57 @@ int main() {
191191
### Java
192192

193193
```java
194+
import java.util.*;
195+
196+
public class Main {
197+
public static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};//下右上左逆时针遍历
198+
199+
public static void bfs(int[][] grid, boolean[][] visited, int x, int y) {
200+
Queue<pair> queue = new LinkedList<pair>();//定义坐标队列,没有现成的pair类,在下面自定义了
201+
queue.add(new pair(x, y));
202+
visited[x][y] = true;//遇到入队直接标记为优先,
203+
// 否则出队时才标记的话会导致重复访问,比如下方节点会在右下顺序的时候被第二次访问入队
204+
while (!queue.isEmpty()) {
205+
int curX = queue.peek().first;
206+
int curY = queue.poll().second;//当前横纵坐标
207+
for (int i = 0; i < 4; i++) {
208+
//顺时针遍历新节点next,下面记录坐标
209+
int nextX = curX + dir[i][0];
210+
int nextY = curY + dir[i][1];
211+
if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) {
212+
continue;
213+
}//去除越界部分
214+
if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
215+
queue.add(new pair(nextX, nextY));
216+
visited[nextX][nextY] = true;//逻辑同上
217+
}
218+
}
219+
}
220+
}
194221

195-
196-
197-
222+
public static void main(String[] args) {
223+
Scanner sc = new Scanner(System.in);
224+
int m = sc.nextInt();
225+
int n = sc.nextInt();
226+
int[][] grid = new int[m][n];
227+
boolean[][] visited = new boolean[m][n];
228+
int ans = 0;
229+
for (int i = 0; i < m; i++) {
230+
for (int j = 0; j < n; j++) {
231+
grid[i][j] = sc.nextInt();
232+
}
233+
}
234+
for (int i = 0; i < m; i++) {
235+
for (int j = 0; j < n; j++) {
236+
if (!visited[i][j] && grid[i][j] == 1) {
237+
ans++;
238+
bfs(grid, visited, i, j);
239+
}
240+
}
241+
}
242+
System.out.println(ans);
243+
}
244+
}
198245
```
199246

200247

‎problems/kamacoder/0099.岛屿的数量深搜.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,52 @@ int main() {
182182
## 其他语言版本
183183

184184
### Java
185+
```java
186+
import java.util.Scanner;
187+
188+
public class Main {
189+
public static int[][] dir ={{0,1},{1,0},{-1,0},{0,-1}};
190+
public static void dfs(boolean[][] visited,int x,int y ,int [][]grid)
191+
{
192+
for (int i = 0; i < 4; i++) {
193+
int nextX=x+dir[i][0];
194+
int nextY=y+dir[i][1];
195+
if(nextY<0||nextX<0||nextX>= grid.length||nextY>=grid[0].length)
196+
continue;
197+
if(!visited[nextX][nextY]&&grid[nextX][nextY]==1)
198+
{
199+
visited[nextX][nextY]=true;
200+
dfs(visited,nextX,nextY,grid);
201+
}
202+
}
203+
}
204+
public static void main(String[] args) {
205+
Scanner sc = new Scanner(System.in);
206+
int m= sc.nextInt();
207+
int n = sc.nextInt();
208+
int[][] grid = new int[m][n];
209+
for (int i = 0; i < m; i++) {
210+
for (int j = 0; j < n; j++) {
211+
grid[i][j]=sc.nextInt();
212+
}
213+
}
214+
boolean[][]visited =new boolean[m][n];
215+
int ans = 0;
216+
for (int i = 0; i < m; i++) {
217+
for (int j = 0; j < n; j++) {
218+
if(!visited[i][j]&&grid[i][j]==1)
219+
{
220+
ans++;
221+
visited[i][j]=true;
222+
dfs(visited,i,j,grid);
223+
}
224+
}
225+
}
226+
System.out.println(ans);
227+
}
228+
}
185229

230+
```
186231
### Python
187232

188233
版本一

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /