问题详见:Island Perimeter
题目是计算一个用二维矩阵表示的海岛的周长。题目描述如下:
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.
Example:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
Answer: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
解题思路:
由于题目中是一个没有“内湖”的海岛,而且是由一个个相邻的正方形网格组成(虽然实际上的海岛比这复杂很多,但是利用微分思想也还是能解释通)。所以只需要把该网格矩阵中1所代表的陆地的边缘找到即可。这里提供两条思路:1.把每个代表陆地的网格块的4个边相加并减去重复的边;2.找到所有代表海洋的与代表陆地的网格

本文介绍了一种使用二维矩阵表示的海岛周长计算方法。通过两种不同的算法思路,详细阐述了如何确定由1(陆地)和0(水)组成的矩阵中海岛的周长。
最低0.47元/天 解锁文章
2万+

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



