forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathislands.cpp
71 lines (60 loc) · 1.39 KB
/
islands.cpp
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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int find_islands(vector<int> v) {
// Sink the whole range
int minimum = v[0];
for(int i = 0; i < v.size(); i++) {
minimum = min(minimum, v[i]);
}
for(int i = 0; i < v.size(); i++) {
v[i] -= minimum;
}
// Check that there's a non-zero element
bool nonzero = false;
for(int i = 0; i < v.size(); i++) {
if(v[i] != 0) {
nonzero = true;
}
}
if(!nonzero) {
return 0;
}
// Start new array of islands
vector<vector<int>> newislands;
vector<int> filler;
newislands.push_back(filler);
// Fill array of islands
for(int i = 0; i < v.size(); i++) {
if(v[i] == 0) {
vector<int> temp;
newislands.push_back(temp);
}
else {
newislands[newislands.size()-1].push_back(v[i]);
}
}
// Count the islands
int total = 0;
for(auto i : newislands) {
if(!i.empty()) {
total += 1;
total += find_islands(i);
}
}
return total;
}
int main() {
int cases;
cin >> cases;
for(int i = 1; i <= cases; i++) {
cin >> i;
vector<int> v;
v.resize(12);
for(int j = 0; j < 12; j++) {
cin >> v[j];
}
cout << i << " " << find_islands(v) << endl;
}
}