-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerimeter.h
44 lines (38 loc) · 1.23 KB
/
Perimeter.h
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
#ifndef CALCULATE_PERIMETER_H
#define CALCULATE_PERIMETER_H
#include <vector>
#include <algorithm>
//using namespace std;
/** Calculates the perimeter of an island
@param islandPerimeter: 2 dimensional array (vector) of the island
@returns: the calculated perimeter of the island
*/
int calc_perimeter(const std::vector<std::vector<int> >& islandPerimeter) {
//Initialize return value
int perimeter = 0;
//Check for 1's
for (int i = 0; i < islandPerimeter.size(); i++) {
for (int j = 0; j < islandPerimeter[i].size(); j++) {
if (islandPerimeter[i][j] == 1) {
//Check if cell is in top row OR has 0 above
if (i == 0 || islandPerimeter[i - 1][j] == 0) {
perimeter++;
}
//Check if cell is in the bottom row or has 0 below
if (i == islandPerimeter.size() - 1 || islandPerimeter[i + 1][j] == 0) {
perimeter++;
}
//Check if cell is all the way to the left or has 0 to the left
if (j == 0 || islandPerimeter[i][j - 1] == 0) {
perimeter++;
}
//Check if cell is all the way to the right or has 0 to the right
if (j == islandPerimeter[i].size() - 1 || islandPerimeter[i][j + 1] == 0) {
perimeter++;
}
}
}
}
return perimeter;
}
#endif