TOP101题解 | BM57#岛屿数量#
岛屿数量
https://www.nowcoder.com/practice/0c9664d1554e466aa107d899418e814e
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* @author Senky
* @date 2023.08.26
* @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1
* 判断岛屿数量
* @param grid char字符型二维数组
* @param gridRowLen int grid数组行数
* @param gridColLen int* grid数组列数
* @return int整型
*/
void DFS(char** grid, int gridRowLen, int gridColLen, int row, int column)
{
/*非法值,以及访问、访问标记则退出*/
if(row < 0 || column < 0 || row >= gridRowLen || column >= gridColLen || grid[row][column] == '0')
{
return;
}
else
{
grid[row][column] = '0';/*访问标记,同海洋*/
DFS(grid, gridRowLen, gridColLen, row - 1, column);/*上*/
DFS(grid, gridRowLen, gridColLen, row + 1, column);/*下*/
DFS(grid, gridRowLen, gridColLen, row, column - 1);/*左*/
DFS(grid, gridRowLen, gridColLen, row, column + 1);/*右*/
}
}
int solve(char** grid, int gridRowLen, int* gridColLen )
{
// write code here
int IslandCount = 0;
for(int row = 0; row < gridRowLen; row++)
{
for(int column = 0; column < gridColLen[row]; column++)
{
if(grid[row][column] == '1')
{
IslandCount++;//岛屿++
DFS(grid, gridRowLen, gridColLen[row], row, column);
}
}
}
return IslandCount;
}
#TOP101#TOP101-BM系列 文章被收录于专栏
系列的题解