-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-numberOfIslands.swift
112 lines (98 loc) · 2.99 KB
/
8-numberOfIslands.swift
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
// Count the number of islands in a 2 dimentional matrix
// An island is defined to a portion of land represented by 1
// Water is represented as 0
// example:
// [
// [1,1,1,1,1,1,1,1,1],
// [1,1,1,1,1,1,1,1,1],
// [1,1,0,0,0,0,0,1,1],
// [1,1,0,0,0,0,0,1,1],
// [1,1,0,0,0,0,0,1,1],
// [1,1,0,0,0,0,0,0,0],
// ]
// result should be 1
// THIS IS A SOLUTION FOR ITERATIVE APPROACH USING A LIST
var landMap = [
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[1,1,0,0,0,0,0,1,1],
[1,1,0,1,1,0,0,1,1],
[1,1,0,0,1,1,0,0,1],
[1,1,0,0,0,0,0,0,0]
]
// SHOW MAP
for i in landMap.indices {
let i = Int(i)
for j in landMap[i].indices {
let j = Int(j)
print("\(landMap[i][j])", separator: " ", terminator: "")
}
print("")
}
struct Tuple: Hashable {
let x: Int
let y: Int
}
func countNumberOfIslands(in island_map: [[Int]]) -> Int {
var stack = Set<Tuple>() {
didSet {
print("Stack = \(stack)")
}
}
var map = island_map
var counter = 0
for i in map.indices {
let i = Int(i)
for j in map[i].indices {
let j = Int(j)
if map[i][j] == 1 {
stack.insert(Tuple(x: i, y: j))
// SHOW MAP
print("")
for i in map.indices {
let i = Int(i)
for j in map[i].indices {
let j = Int(j)
print("\(map[i][j])", separator: " ", terminator: "")
}
print("")
}
while !stack.isEmpty {
let tuple = stack.removeFirst()
let new_i = tuple.x
let new_j = tuple.y
map[new_i][new_j] = 0
//left
if new_i > 0 {
if map[new_i - 1][new_j] == 1 {
stack.insert(Tuple(x: new_i - 1, y: new_j))
}
}
//up
if new_j > 0 {
if map[new_i][new_j - 1] == 1 {
stack.insert(Tuple(x: new_i, y: new_j - 1))
}
}
//right
if new_i < map.count - 1 {
if map[new_i + 1][new_j] == 1 {
stack.insert(Tuple(x: new_i + 1, y: new_j))
}
}
//down
if new_j < map[new_i].count - 1 {
if map[new_i][new_j + 1] == 1 {
stack.insert(Tuple(x: new_i, y: new_j + 1))
}
}
}
counter += 1
print("Counter = \(counter)")
}
}
}
return counter
}
let result = countNumberOfIslands(in: landMap)
print("Number os islands is = \(result)")