-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path463-IslandPerimeter.go
More file actions
118 lines (105 loc) · 3.7 KB
/
463-IslandPerimeter.go
File metadata and controls
118 lines (105 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
// 463. Island Perimeter
// You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
// Grid cells are connected horizontally/vertically (not diagonally).
// The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
// The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island.
// One cell is a square with side length 1.
// The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" />
// Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
// Output: 16
// Explanation: The perimeter is the 16 yellow stripes in the image above.
// Example 2:
// <img src="" />
// Input: grid = [[1]]
// Output: 4
// Example 3:
// <img src="" />
// Input: grid = [[1,0]]
// Output: 4
// Constraints:
// row == grid.length
// col == grid[i].length
// 1 <= row, col <= 100
// grid[i][j] is 0 or 1.
// There is exactly one island in grid.
import "fmt"
func islandPerimeter(grid [][]int) int {
res := 0
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
// 判断四周边界的情况依次加一
if grid[i][j] == 1 {
if i-1 < 0 || grid[i-1][j] == 0 {
res++
}
if i + 1 >= len(grid) || grid[i+1][j] == 0 {
res++
}
if j - 1 < 0 || grid[i][j-1] == 0 {
res++
}
if j + 1 >= len(grid[0]) || grid[i][j+1] == 0 {
res++
}
}
}
}
return res
}
// dfs
func islandPerimeter1(grid [][]int) int {
var dfs func(x, y int, grid [][]int) int
dfs = func(x, y int, grid [][]int) int {
if x < 0 || x > len(grid)-1 || y < 0 || y > len(grid[0])-1 {
return 1
}
if grid[x][y] == 0 {
return 1
}
if grid[x][y] == 2 {
return 0
}
grid[x][y] = 2
return dfs(x+1, y, grid) + dfs(x-1, y, grid) + dfs(x, y+1, grid) + dfs(x, y-1, grid)
}
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if grid[i][j] == 1 {
return dfs(i, j, grid)
}
}
}
return 0
}
func islandPerimeter2(grid [][]int) int {
dirs := [][]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}}
rows, cols, res := len(grid), len(grid[0]), 0
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
if grid[row][col] == 1 {
for _, dir := range dirs {
r, c := row + dir[0], col + dir[1]
// Check to see if the neighbour is an edge tile or water.
if r < 0 || r >= rows || c < 0 || c >= cols || grid[r][c] == 0 {
res++
}
}
}
}
}
return res
}
func main() {
fmt.Println(islandPerimeter([][]int {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}})) // 16
fmt.Println(islandPerimeter([][]int{{1}})) // 4
fmt.Println(islandPerimeter([][]int{{1,0}})) // 4
fmt.Println(islandPerimeter1([][]int {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}})) // 16
fmt.Println(islandPerimeter1([][]int{{1}})) // 4
fmt.Println(islandPerimeter1([][]int{{1,0}})) // 4
fmt.Println(islandPerimeter2([][]int {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}})) // 16
fmt.Println(islandPerimeter2([][]int{{1}})) // 4
fmt.Println(islandPerimeter2([][]int{{1,0}})) // 4
}