mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
Merge pull request #2635 from sxdtywm/patch-1
feat:add algorithm of go python js java etc.
This commit is contained in:
@ -190,60 +190,164 @@ int main() {
|
|||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
static int[][] dir = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} }; // 四个方向
|
||||||
|
|
||||||
|
public static void dfs(int[][] grid, boolean[][] visited, int x, int y) {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nextx = x + dir[i][0];
|
||||||
|
int nexty = y + dir[i][1];
|
||||||
|
if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过
|
||||||
|
if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 没有访问过的 同时 是陆地的
|
||||||
|
visited[nextx][nexty] = true;
|
||||||
|
dfs(grid, visited, nextx, nexty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
int m = scanner.nextInt();
|
||||||
|
int[][] grid = new int[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
grid[i][j] = scanner.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean[][] visited = new boolean[n][m];
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (!visited[i][j] && grid[i][j] == 1) {
|
||||||
|
visited[i][j] = true;
|
||||||
|
result++; // 遇到没访问过的陆地,+1
|
||||||
|
dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from collections import deque
|
|
||||||
|
|
||||||
# 四个方向
|
def dfs(grid, visited, x, y):
|
||||||
position = [[0, 1], [1, 0], [0, -1], [-1, 0]]
|
dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 四个方向
|
||||||
|
for d in dir:
|
||||||
|
nextx, nexty = x + d[0], y + d[1]
|
||||||
|
if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]):
|
||||||
|
if not visited[nextx][nexty] and grid[nextx][nexty] == 1: # 没有访问过的 同时 是陆地的
|
||||||
|
visited[nextx][nexty] = True
|
||||||
|
dfs(grid, visited, nextx, nexty)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
n, m = map(int, input().split())
|
||||||
|
grid = [list(map(int, input().split())) for _ in range(n)]
|
||||||
|
visited = [[False] * m for _ in range(n)]
|
||||||
|
|
||||||
|
result = 0
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(m):
|
||||||
|
if not visited[i][j] and grid[i][j] == 1:
|
||||||
|
visited[i][j] = True
|
||||||
|
result += 1 # 遇到没访问过的陆地,+1
|
||||||
|
dfs(grid, visited, i, j) # 将与其链接的陆地都标记上 True
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
def bfs(grid, visited, x, y):
|
|
||||||
"""
|
|
||||||
广度优先搜索对陆地进行标记
|
|
||||||
"""
|
|
||||||
|
|
||||||
que = deque() # 创建队列
|
|
||||||
|
|
||||||
# 标记当前节点并加入队列
|
|
||||||
visited[x][y] = True
|
|
||||||
que.append([x, y])
|
|
||||||
|
|
||||||
while que:
|
|
||||||
cur_x, cur_y = que.popleft() # 取出队首节点
|
|
||||||
for i, j in position:
|
|
||||||
next_x = cur_x + i
|
|
||||||
next_y = cur_y + j
|
|
||||||
# 下一节点下标越界,跳过
|
|
||||||
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
|
|
||||||
continue
|
|
||||||
# 下一节点是陆地且未被访问,标记节点并加入队列
|
|
||||||
if grid[next_x][next_y] == 1 and not visited[next_x][next_y]:
|
|
||||||
visited[next_x][next_y] = True
|
|
||||||
que.append([next_x, next_y])
|
|
||||||
|
|
||||||
|
|
||||||
n, m = map(int, input().split())
|
|
||||||
# 邻接矩阵
|
|
||||||
grid = []
|
|
||||||
for i in range(n):
|
|
||||||
grid.append(list(map(int, input().split())))
|
|
||||||
|
|
||||||
visited = [[False] * m for _ in range(n)] # 访问表
|
|
||||||
|
|
||||||
res = 0
|
|
||||||
for i in range(n):
|
|
||||||
for j in range(m):
|
|
||||||
if grid[i][j] == 1 and not visited[i][j]:
|
|
||||||
res += 1
|
|
||||||
bfs(grid, visited, i, j)
|
|
||||||
|
|
||||||
print(res)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dir = [4][2]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向
|
||||||
|
|
||||||
|
func dfs(grid [][]int, visited [][]bool, x, y int) {
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
nextx := x + dir[i][0]
|
||||||
|
nexty := y + dir[i][1]
|
||||||
|
if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) {
|
||||||
|
continue // 越界了,直接跳过
|
||||||
|
}
|
||||||
|
if !visited[nextx][nexty] && grid[nextx][nexty] == 1 { // 没有访问过的 同时 是陆地的
|
||||||
|
visited[nextx][nexty] = true
|
||||||
|
dfs(grid, visited, nextx, nexty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
var n, m int
|
||||||
|
fmt.Scanf("%d %d", &n, &m)
|
||||||
|
|
||||||
|
grid := make([][]int, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
grid[i] = make([]int, m)
|
||||||
|
line, _ := reader.ReadString('\n')
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
elements := strings.Split(line, " ")
|
||||||
|
for j := 0; j < m; j++ {
|
||||||
|
grid[i][j], _ = strconv.Atoi(elements[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
visited := make([][]bool, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
visited[i] = make([]bool, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := 0
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
for j := 0; j < m; j++ {
|
||||||
|
if !visited[i][j] && grid[i][j] == 1 {
|
||||||
|
visited[i][j] = true
|
||||||
|
result++ // 遇到没访问过的陆地,+1
|
||||||
|
dfs(grid, visited, i, j) // 将与其链接的陆地都标记上 true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
@ -333,12 +437,62 @@ const bfs = (graph, visited, x, y) => {
|
|||||||
|
|
||||||
### PhP
|
### PhP
|
||||||
|
|
||||||
|
```PHP
|
||||||
|
|
||||||
|
<?php
|
||||||
|
|
||||||
|
function dfs($grid, &$visited, $x, $y) {
|
||||||
|
$dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
|
||||||
|
foreach ($dir as $d) {
|
||||||
|
$nextx = $x + $d[0];
|
||||||
|
$nexty = $y + $d[1];
|
||||||
|
if ($nextx < 0 || $nextx >= count($grid) || $nexty < 0 || $nexty >= count($grid[0])) {
|
||||||
|
continue; // 越界了,直接跳过
|
||||||
|
}
|
||||||
|
if (!$visited[$nextx][$nexty] && $grid[$nextx][$nexty] == 1) { // 没有访问过的 同时 是陆地的
|
||||||
|
$visited[$nextx][$nexty] = true;
|
||||||
|
dfs($grid, $visited, $nextx, $nexty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
fscanf(STDIN, "%d %d", $n, $m);
|
||||||
|
$grid = [];
|
||||||
|
for ($i = 0; $i < $n; $i++) {
|
||||||
|
$grid[$i] = array_map('intval', explode(' ', trim(fgets(STDIN))));
|
||||||
|
}
|
||||||
|
|
||||||
|
$visited = array_fill(0, $n, array_fill(0, $m, false));
|
||||||
|
|
||||||
|
$result = 0;
|
||||||
|
for ($i = 0; $i < $n; $i++) {
|
||||||
|
for ($j = 0; $j < $m; $j++) {
|
||||||
|
if (!$visited[$i][$j] && $grid[$i][$j] == 1) {
|
||||||
|
$visited[$i][$j] = true;
|
||||||
|
$result++; // 遇到没访问过的陆地,+1
|
||||||
|
dfs($grid, $visited, $i, $j); // 将与其链接的陆地都标记上 true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $result . PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Swift
|
### Swift
|
||||||
|
|
||||||
### Scala
|
### Scala
|
||||||
|
|
||||||
### C#
|
### C#
|
||||||
|
|
||||||
|
|
||||||
### Dart
|
### Dart
|
||||||
|
|
||||||
### C
|
### C
|
||||||
|
Reference in New Issue
Block a user