Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int islandPerimeter(vector<vector<int>>& grid) {
- int n = grid.size();
- int m = grid[0].size();
- int squares = 0;
- int di[] = {0, 0, -1, 1};
- int dj[] = {-1, 1, 0, 0};
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- if(grid[i][j] == 1) {
- squares += 4;
- for(int k = 0; k < 4; k++) {
- int ti = i + di[k];
- int tj = j + dj[k];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and grid[ti][tj] == 1) {
- squares--;
- }
- }
- }
- }
- }
- return squares;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement