-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path463.cpp
More file actions
38 lines (35 loc) · 897 Bytes
/
Copy path463.cpp
File metadata and controls
38 lines (35 loc) · 897 Bytes
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
#include "common.h"
using namespace std;
class Solution {
public:
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int islandPerimeter(vector<vector<int>>& grid) {
int perimeter = 0;
for (size_t i = 0; i < grid.size(); i++) {
for (size_t j = 0; j < grid[i].size(); j++) {
if (grid[i][j] == 1) {
for (size_t k = 0; k < 4; k++) {
int x = i + dx[k];
int y = j + dy[k];
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size()) {
perimeter += 1;
continue;
}
if (grid[x][y] != 1) {
perimeter += 1;
}
}
}
}
}
return perimeter;
}
};
int main() {
Solution s;
vector<vector<int>> v = {
{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}};
cout << s.islandPerimeter(v) << endl;
return 0;
}