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 957c60d

Browse files
authored
Update 0100.岛屿的最大面积.md of php and go
Update 0100.岛屿的最大面积.md of php and go
1 parent 06997eb commit 957c60d

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

‎problems/kamacoder/0100.岛屿的最大面积.md‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,72 @@ print(result)
322322

323323
### Go
324324

325+
``` go
326+
327+
package main
328+
329+
import (
330+
"fmt"
331+
)
332+
333+
var count int
334+
var dir = [][]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向
335+
336+
func dfs(grid [][]int, visited [][]bool, x, y int) {
337+
for i := 0; i < 4; i++ {
338+
nextx := x + dir[i][0]
339+
nexty := y + dir[i][1]
340+
if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) {
341+
continue // 越界了,直接跳过
342+
}
343+
if !visited[nextx][nexty] && grid[nextx][nexty] == 1 { // 没有访问过的 同时 是陆地的
344+
visited[nextx][nexty] = true
345+
count++
346+
dfs(grid, visited, nextx, nexty)
347+
}
348+
}
349+
}
350+
351+
func main() {
352+
var n, m int
353+
fmt.Scan(&n, &m)
354+
355+
grid := make([][]int, n)
356+
for i := 0; i < n; i++ {
357+
grid[i] = make([]int, m)
358+
for j := 0; j < m; j++ {
359+
fmt.Scan(&grid[i][j])
360+
}
361+
}
362+
363+
visited := make([][]bool, n)
364+
for i := 0; i < n; i++ {
365+
visited[i] = make([]bool, m)
366+
}
367+
368+
result := 0
369+
for i := 0; i < n; i++ {
370+
for j := 0; j < m; j++ {
371+
if !visited[i][j] && grid[i][j] == 1 {
372+
count = 1 // 因为dfs处理下一个节点,所以这里遇到陆地了就先计数,dfs处理接下来的相邻陆地
373+
visited[i][j] = true
374+
dfs(grid, visited, i, j)
375+
if count > result {
376+
result = count
377+
}
378+
}
379+
}
380+
}
381+
382+
fmt.Println(result)
383+
}
384+
385+
386+
387+
```
388+
389+
390+
325391
### Rust
326392

327393
### Javascript
@@ -420,6 +486,65 @@ const bfs = (graph, visited, x, y) => {
420486

421487
### PhP
422488

489+
``` php
490+
491+
<?php
492+
493+
function dfs(&$grid, &$visited, $x, $y, &$count, &$dir) {
494+
for ($i = 0; $i < 4; $i++) {
495+
$nextx = $x + $dir[$i][0];
496+
$nexty = $y + $dir[$i][1];
497+
if ($nextx < 0 || $nextx >= count($grid) || $nexty < 0 || $nexty >= count($grid[0])) continue; // 越界了,直接跳过
498+
if (!$visited[$nextx][$nexty] && $grid[$nextx][$nexty] == 1) { // 没有访问过的 同时 是陆地的
499+
$visited[$nextx][$nexty] = true;
500+
$count++;
501+
dfs($grid, $visited, $nextx, $nexty, $count, $dir);
502+
}
503+
}
504+
}
505+
506+
// Main function
507+
function main() {
508+
$input = trim(fgets(STDIN));
509+
list($n, $m) = explode(' ', $input);
510+
511+
$grid = [];
512+
for ($i = 0; $i < $n; $i++) {
513+
$input = trim(fgets(STDIN));
514+
$grid[] = array_map('intval', explode(' ', $input));
515+
}
516+
517+
$visited = [];
518+
for ($i = 0; $i < $n; $i++) {
519+
$visited[] = array_fill(0, $m, false);
520+
}
521+
522+
$result = 0;
523+
$count = 0;
524+
$dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
525+
526+
for ($i = 0; $i < $n; $i++) {
527+
for ($j = 0; $j < $m; $j++) {
528+
if (!$visited[$i][$j] && $grid[$i][$j] == 1) {
529+
$count = 1; // 因为dfs处理下一个节点,所以这里遇到陆地了就先计数,dfs处理接下来的相邻陆地
530+
$visited[$i][$j] = true;
531+
dfs($grid, $visited, $i, $j, $count, $dir); // 将与其链接的陆地都标记上 true
532+
$result = max($result, $count);
533+
}
534+
}
535+
}
536+
537+
echo $result . "\n";
538+
}
539+
540+
main();
541+
542+
?>
543+
544+
545+
```
546+
547+
423548
### Swift
424549

425550
### Scala

0 commit comments

Comments
(0)

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