-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_of_islands.go
170 lines (149 loc) · 3.29 KB
/
number_of_islands.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// https://leetcode.com/problems/number-of-islands/description/
package main
import (
"encoding/json"
"errors"
)
//lint:ignore U1000 Ignore
func getgrid() [][]byte {
// Given input string
input := `[["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]`
var arr [][]string
err := json.Unmarshal([]byte(input), &arr)
if err != nil {
panic(err)
}
var result [][]byte
for _, row := range arr {
var byteRow []byte
for _, str := range row {
byteRow = append(byteRow, str[0]) // assuming each string is a single character "0" or "1"
}
result = append(result, byteRow)
}
return result
}
// func main() {
// grid := getgrid()
// islands := numIslands(grid)
// print(islands)
// }
//lint:ignore U1000 Ignore
func numIslands(grid [][]byte) int {
make_byte_array_non_retarded(&grid)
g := *NewGrid(grid)
island_count := g.paint_islands()
return int(island_count)
}
func make_byte_array_non_retarded(arr *[][]byte) {
m := len(*arr)
n := len((*arr)[0])
for i := range m {
for j := range n {
if (*arr)[i][j] == 48 {
(*arr)[i][j] = 0
} else {
(*arr)[i][j] = 1
}
}
}
}
type Grid struct {
arr [][]byte
m int
n int
}
func NewGrid(arr [][]byte) *Grid {
m := len(arr)
n := len(arr[0])
return &Grid{arr: arr, m: m, n: n}
}
type Coordinate struct {
x int
y int
val *byte
}
type CoordinateQueue struct {
items []Coordinate
}
func (cq *CoordinateQueue) enqueue(g *Grid, x, y int) bool {
coordinate := Coordinate{x: x, y: y, val: &g.arr[x][y]}
for _, item := range cq.items {
if item.x == coordinate.x && item.y == coordinate.y {
return false
}
}
cq.items = append(cq.items, coordinate)
return true
}
func (cq *CoordinateQueue) dequeue() (Coordinate, error) {
if len(cq.items) < 1 {
return Coordinate{}, errors.New("queue is empty")
}
popped := cq.items[0]
cq.items = cq.items[1:]
return popped, nil
}
func (g *Grid) valid_neighboors(x, y int) []Coordinate {
list := []Coordinate{}
if x > 0 {
if g.arr[x-1][y] == 1 {
list = append(list, Coordinate{x: x - 1, y: y, val: &g.arr[x-1][y]})
}
}
if y > 0 {
if g.arr[x][y-1] == 1 {
list = append(list, Coordinate{x: x, y: y - 1, val: &g.arr[x][y-1]})
}
}
if x < g.m-1 {
if g.arr[x+1][y] == 1 {
list = append(list, Coordinate{x: x + 1, y: y, val: &g.arr[x+1][y]})
}
}
if y < g.n-1 {
if g.arr[x][y+1] == 1 {
list = append(list, Coordinate{x: x, y: y + 1, val: &g.arr[x][y+1]})
}
}
return list
}
func (g *Grid) mark_neighboors(val byte, cq *CoordinateQueue) {
cont := true
for cont {
popped, err := cq.dequeue()
if err == nil {
*popped.val = val
for _, n := range g.valid_neighboors(popped.x, popped.y) {
if *n.val == 1 {
cq.enqueue(g, n.x, n.y)
}
}
} else {
cont = false
}
}
}
func (g *Grid) get_coordinate(x, y int) Coordinate {
return Coordinate{x: x, y: y, val: &g.arr[x][y]}
}
func (g *Grid) paint_islands() int32 {
var marking byte = 1
var islands int32 = 0
var coordinate_queue CoordinateQueue = CoordinateQueue{}
for i := range g.m {
for j := range g.n {
if g.arr[i][j] == 1 {
marking++
islands++
if marking == 255 {
marking = 2
}
coord := g.get_coordinate(i, j)
coordinate_queue.enqueue(g, coord.x, coord.y)
g.mark_neighboors(marking, &coordinate_queue)
}
}
}
return islands
}