//没有额外单独格子干扰情况
int islandPerimeter(int **grid, int gridSize, int* gridColSize)
{
int len = 0;
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < *gridColSize; j++) {
if (grid[i][j] == 1) {
(i == 0 || grid[i - 1][j] == 0) ? len++ : ' ';
(i == gridSize - 1 || grid[i + 1][j] == 0) ? len++ : ' ';
(j == 0 || grid[i][j - 1] == 0) ? len++ : ' ';
(j == (*gridColSize) - 1 || grid[i][j + 1] == 0) ? len++ : ' ';
}
}
}
return len;
}
//有额外格子干扰情况
int islandPerimeter(int** grid, int gridSize, int* gridColSize){
int i,j,perimeter=0,num,count=0;
for (i=0; i<gridSize; i++)
{
for (j=0; j<gridColSize[i]; j++)
{
num=4;
if (grid[i][j])
{
count++;
if (j-1>=0 && grid[i][j-1]) num--;
if (j+1<gridColSize[i] && grid[i][j+1]) num--;
if (i-1>=0 && grid[i-1][j]) num--;
if (i+1<gridSize && grid[i+1][j]) num--;
perimeter += (num==4)? 0: num;
}
}
}
return (count != 1)? perimeter: 4;
}