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 8d29bef

Browse files
authored
feat:add algorithm of go python js java etc.
feat:add algorithm of go python js java etc.
1 parent b093b61 commit 8d29bef

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

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

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,164 @@ int main() {
190190

191191
### Java
192192

193+
```java
194+
195+
import java.util.Scanner;
196+
197+
public class Main {
198+
static int[][] dir = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} }; // 四个方向
199+
200+
public static void dfs(int[][] grid, boolean[][] visited, int x, int y) {
201+
for (int i = 0; i < 4; i++) {
202+
int nextx = x + dir[i][0];
203+
int nexty = y + dir[i][1];
204+
if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过
205+
if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 没有访问过的 同时 是陆地的
206+
visited[nextx][nexty] = true;
207+
dfs(grid, visited, nextx, nexty);
208+
}
209+
}
210+
}
211+
212+
public static void main(String[] args) {
213+
Scanner scanner = new Scanner(System.in);
214+
int n = scanner.nextInt();
215+
int m = scanner.nextInt();
216+
int[][] grid = new int[n][m];
217+
for (int i = 0; i < n; i++) {
218+
for (int j = 0; j < m; j++) {
219+
grid[i][j] = scanner.nextInt();
220+
}
221+
}
222+
223+
boolean[][] visited = new boolean[n][m];
224+
225+
int result = 0;
226+
for (int i = 0; i < n; i++) {
227+
for (int j = 0; j < m; j++) {
228+
if (!visited[i][j] && grid[i][j] == 1) {
229+
visited[i][j] = true;
230+
result++; // 遇到没访问过的陆地,+1
231+
dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
232+
}
233+
}
234+
}
235+
236+
System.out.println(result);
237+
}
238+
}
239+
240+
241+
242+
```
243+
244+
193245
### Python
194246

247+
```python
248+
249+
def dfs(grid, visited, x, y):
250+
dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 四个方向
251+
for d in dir:
252+
nextx, nexty = x + d[0], y + d[1]
253+
if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]):
254+
if not visited[nextx][nexty] and grid[nextx][nexty] == 1: # 没有访问过的 同时 是陆地的
255+
visited[nextx][nexty] = True
256+
dfs(grid, visited, nextx, nexty)
257+
258+
def main():
259+
n, m = map(int, input().split())
260+
grid = [list(map(int, input().split())) for _ in range(n)]
261+
visited = [[False] * m for _ in range(n)]
262+
263+
result = 0
264+
for i in range(n):
265+
for j in range(m):
266+
if not visited[i][j] and grid[i][j] == 1:
267+
visited[i][j] = True
268+
result += 1 # 遇到没访问过的陆地,+1
269+
dfs(grid, visited, i, j) # 将与其链接的陆地都标记上 True
270+
271+
print(result)
272+
273+
if __name__ == "__main__":
274+
main()
275+
276+
277+
278+
```
279+
280+
195281
### Go
196282

283+
```go
284+
285+
package main
286+
287+
import (
288+
"bufio"
289+
"fmt"
290+
"os"
291+
"strconv"
292+
"strings"
293+
)
294+
295+
var dir = [4][2]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向
296+
297+
func dfs(grid [][]int, visited [][]bool, x, y int) {
298+
for i := 0; i < 4; i++ {
299+
nextx := x + dir[i][0]
300+
nexty := y + dir[i][1]
301+
if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) {
302+
continue // 越界了,直接跳过
303+
}
304+
if !visited[nextx][nexty] && grid[nextx][nexty] == 1 { // 没有访问过的 同时 是陆地的
305+
visited[nextx][nexty] = true
306+
dfs(grid, visited, nextx, nexty)
307+
}
308+
}
309+
}
310+
311+
func main() {
312+
reader := bufio.NewReader(os.Stdin)
313+
var n, m int
314+
fmt.Scanf("%d %d", &n, &m)
315+
316+
grid := make([][]int, n)
317+
for i := 0; i < n; i++ {
318+
grid[i] = make([]int, m)
319+
line, _ := reader.ReadString('\n')
320+
line = strings.TrimSpace(line)
321+
elements := strings.Split(line, " ")
322+
for j := 0; j < m; j++ {
323+
grid[i][j], _ = strconv.Atoi(elements[j])
324+
}
325+
}
326+
327+
visited := make([][]bool, n)
328+
for i := 0; i < n; i++ {
329+
visited[i] = make([]bool, m)
330+
}
331+
332+
result := 0
333+
for i := 0; i < n; i++ {
334+
for j := 0; j < m; j++ {
335+
if !visited[i][j] && grid[i][j] == 1 {
336+
visited[i][j] = true
337+
result++ // 遇到没访问过的陆地,+1
338+
dfs(grid, visited, i, j) // 将与其链接的陆地都标记上 true
339+
}
340+
}
341+
}
342+
343+
fmt.Println(result)
344+
}
345+
346+
347+
```
348+
349+
350+
197351
### Rust
198352

199353
### Javascript
@@ -283,12 +437,62 @@ const bfs = (graph, visited, x, y) => {
283437

284438
### PhP
285439

440+
```PHP
441+
442+
<?php
443+
444+
function dfs($grid, &$visited, $x, $y) {
445+
$dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
446+
foreach ($dir as $d) {
447+
$nextx = $x + $d[0];
448+
$nexty = $y + $d[1];
449+
if ($nextx < 0 || $nextx >= count($grid) || $nexty < 0 || $nexty >= count($grid[0])) {
450+
continue; // 越界了,直接跳过
451+
}
452+
if (!$visited[$nextx][$nexty] && $grid[$nextx][$nexty] == 1) { // 没有访问过的 同时 是陆地的
453+
$visited[$nextx][$nexty] = true;
454+
dfs($grid, $visited, $nextx, $nexty);
455+
}
456+
}
457+
}
458+
459+
function main() {
460+
fscanf(STDIN, "%d %d", $n, $m);
461+
$grid = [];
462+
for ($i = 0; $i < $n; $i++) {
463+
$grid[$i] = array_map('intval', explode(' ', trim(fgets(STDIN))));
464+
}
465+
466+
$visited = array_fill(0, $n, array_fill(0, $m, false));
467+
468+
$result = 0;
469+
for ($i = 0; $i < $n; $i++) {
470+
for ($j = 0; $j < $m; $j++) {
471+
if (!$visited[$i][$j] && $grid[$i][$j] == 1) {
472+
$visited[$i][$j] = true;
473+
$result++; // 遇到没访问过的陆地,+1
474+
dfs($grid, $visited, $i, $j); // 将与其链接的陆地都标记上 true
475+
}
476+
}
477+
}
478+
479+
echo $result . PHP_EOL;
480+
}
481+
482+
main();
483+
?>
484+
485+
486+
```
487+
488+
286489
### Swift
287490

288491
### Scala
289492

290493
### C#
291494

495+
292496
### Dart
293497

294498
### C

0 commit comments

Comments
(0)

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