/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 判断岛屿数量 * @param grid char字符型二维数组 * @param gridRowLen int grid数组行数 * @param gridColLen int* grid数组列数 * @return int整型 */ void dfs(char** grid, int gridRowLen, int* gridColLen, int row, int col) { // 如果超越了边界,直接返回 if(row < 0 || row >= gridRowLen || col < 0 || col >= gridColLen[row]) return; // 如果当前位置是0,则返回 if(grid[row][col] == '0') return; // 否则的话,标记为0,然后继续递归访问上下左右的岛屿 grid[row][col] = '0'; dfs(grid, gridRowLen, gridColLen, row + 1, col); dfs(grid, gridRowLen, gridColLen, row - 1, col); dfs(grid, gridRowLen, gridColLen, row, col + 1); dfs(grid, gridRowLen, gridColLen, row, col - 1); } #include <stdint.h> int solve(char** grid, int gridRowLen, int* gridColLen ) { // write code here int island = 0; for(int i = 0; i < gridRowLen; i++) { for(int j = 0; j < gridColLen[i]; j++) { if(grid[i][j] == '1') { island++; dfs(grid, gridRowLen, gridColLen, i, j); } } } return island; }