# 463.Island Perimeter

**463.Island Perimeter**

难度:Easy

> 给定一个包含 0 和 1 的二维网格地图，其中 1 表示陆地 0 表示水域。

网格中的格子水平和垂直方向相连（对角线方向不相连）。整个网格被水完全包围，但其中恰好有一个岛屿（或者说，一个或多个表示陆地的格子相连组成的岛屿）。

岛屿中没有“湖”（“湖” 指水域在岛屿内部且不和岛屿周围的水相连）。格子是边长为 1 的正方形。网格为长方形，且宽度和高度均不超过 100 。计算这个岛屿的周长。

```
示例 :

输入:
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

输出: 16
```

解释: 它的周长是下面图片中的 16 个黄色的边：

![island.png](https://i.loli.net/2019/03/30/5c9ed72e361c6.png)

方法：计算周长，可以看是否在外周，即周围是否有水，存在水，则可计入周长。代码如下：

```
class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid) {
        int sum=0;
        int r=grid.size();
        int c=grid[0].size();
        for(int i=0;i<r;i++)
            for(int j=0;j<c;j++)
            {
                if(grid[i][j])
                {
                    sum += 4- (i>0 && grid[i-1][j])  - (i<r-1 &&grid[i+1][j]) - (j>0  && grid[i][j-1])  - (j<c-1 && grid[i][j+1]);
                }
            }
        return sum;

    }
};
```

> 执行用时 : 112 ms, 在Island Perimeter的C++提交中击败了82.22% 的用户 内存消耗 : 16 MB, 在Island Perimeter的C++提交中击败了91.01% 的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/463.island_perimeter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
