aboutsummaryrefslogtreecommitdiff
path: root/island_perimeter.c
blob: 8e898cae5348269b54271f6c5780a46b35056725 (plain)
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
//463. Island Perimeter
#include "leetcode.h"

/*
 * given  a 'row * col' grid representing a map where 'grid[i][j] = 1'
 * represents land and 'grid[i][j] = 0' represents water. grid cells are
 * connected horizontally/vertically (not diagonally). the 'gird' is completely
 * surrounded by water, and there is exactly one island. the island does not
 * have lakes meaning the water inside is not connected to the water around the
 * island. one cell is a square with side length 1. the grid is rectangular
 * width and height don't exceed 100. determine the perimeter of the island.
 */

int edge_sum(int **grid, int row, int col, int grid_row_size,
             int grid_col_size) {
  int total = 0;
  if (row - 1 < 0 || !grid[row - 1][col])
    total++;
  if (row + 1 == grid_row_size || !grid[row + 1][col])
    total++;
  if (col - 1 < 0 || !grid[row][col - 1])
    total++;
  if (col + 1 == grid_col_size || !grid[row][col + 1])
    total++;
  return total;
}

int islandPerimeter(int **grid, int gridSize, int *gridColSize) {
  int ans = 0;
  for (int i = 0; i < gridSize; ++i)
    for (int j = 0; j < *gridColSize; ++j)
      if (grid[i][j])
        ans += edge_sum(grid, i, j, gridSize, *gridColSize);
  return ans;
}