原题:
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected
horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't 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.
C++解法:
int num(vector>& grid, int i, int j)
{
int t = 0;
vector>::size_type m = grid.size();
vector::size_type n = grid[0].size();
if(i>0&&grid[i-1][j]==1) t++;
if(i0&&grid[i][j-1]==1) t++;
if(j>& grid) {
if(grid.empty())
return 0;
int res = 0;
vector tmp;
for(vector>::size_type i = 0; i != grid.size(); i++)
for(vector::size_type j=0;j!=grid[i].size();++j)
if(grid[i][j]==1)
res += 4-num(grid,i,j);
return res;
}
本文介绍了一个C++解决方案,用于计算二维网格中岛屿的周长。网格由1(陆地)和0(水域)组成,且网格完全被水包围。文章详细展示了如何通过遍历网格并检查每个陆地单元格的相邻单元格来确定周长。
101

被折叠的 条评论
为什么被折叠?



