首页 > 试题广场 >

岛屿数量

[编程题]岛屿数量
  • 热度指数:111709 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。
岛屿: 相邻陆地可以组成一个岛屿(相邻:上下左右) 判断岛屿个数。
例如:
输入
[
[1,1,0,0,0],
[0,1,0,1,1],
[0,0,0,1,1],
[0,0,0,0,0],
[0,0,1,1,1]
]
对应的输出为3
(注:存储的01数据其实是字符'0','1')
示例1

输入

[[1,1,0,0,0],[0,1,0,1,1],[0,0,0,1,1],[0,0,0,0,0],[0,0,1,1,1]]

输出

3
示例2

输入

[[0]]

输出

0
示例3

输入

[[1,1],[1,1]]

输出

1

备注:
01矩阵范围<=200*200
function solve( grid ) {
    let nums = 0
    let visited = Array.from(Array(grid.length), ()=>Array(grid[0].length).fill(false))
    for(let i=0; i<grid.length; i++) {
        for(let j=0; j<grid[0].length; j++) {
            if(grid[i][j]=== 1 && visited[i][j] === false) {
                nums++
                backFunc(i, j)
            }
        }
    }
    function backFunc(i, j) {
        if( i<0 || i>grid.length-1 || j<0 || j>grid[0].length-1 || visited[i][j]===true) {
            return 
        }
        if(grid[i][j]===1) {
            visited[i][j] = true
            backFunc(i-1, j)
            backFunc(i+1, j)
            backFunc(i, j-1)
            backFunc(i, j+1)
        }
    }
    return nums
}

发表于 2023-06-26 18:31:57 回复(1)

function solve( grid ) {
    // write code here
    let count = 0
    for(let i = 0; i < grid.length; i ++){
        for(let j = 0; j < grid[0].length; j ++){
            // 遇到1,则对与其相连的1置为0(沉岛)
            if(grid[i][j] === '1'){
                count ++
                turnZero(i, j, grid)
            }
        }
    }
    return count
    
    // 沉岛
    function turnZero(i, j, grid){
        if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] === '0') return
        grid[i][j] = '0'
        turnZero(i, j + 1, grid)
        turnZero(i, j - 1, grid)
        turnZero(i - 1, j, grid)
        turnZero(i + 1, j, grid)
    }
}


发表于 2022-04-15 16:22:54 回复(0)
function solve( grid ) {
    // write code here
    let count = 0;
    
    function dfs(row,col) {
        if(row<0 || row>=grid.length || col <0 || col>=grid[0].length 
           || grid[row][col]==="0" ) { 
            return ;
        }
        grid[row][col] = "0";
        dfs(row+1,col);
        dfs(row-1,col);
        dfs(row,col-1);
        dfs(row,col+1);
    }
    
    for(let row =0; row<grid.length; row++) {
        for(let col = 0; col<grid[0].length; col ++) {
            if(grid[row][col] === "1") {
                count++;
                dfs(row,col);
            }
        }
    }
    return count;
}

发表于 2022-03-26 16:14:23 回复(0)
呃呃,看了半天才发现是字符不是数字😄
发表于 2022-03-14 19:48:36 回复(0)
/**
 * 判断岛屿数量
 * @param grid char字符型二维数组 
 * @return int整型
 */
function solve( grid ) {
    // write code here
    let m = grid.length;
    let n = grid[0].length;
    
    let res = 0;
    for(let i=0;i<m;i++){
        for(let j=0;j<n;j++){
            if(grid[i][j]==1){
               //求数量
                res++;  
                dfs(grid,i,j);
                // 求面积
                //res = Math.max(res,dfs(grid,i,j));
                //求周长,仅有一个岛屿
               // res = dfs(grid,i,j);
            }
        }
    }
    return res;
}
function dfs(grid,m,n){
    if(!isArea(grid,m,n)){
      return;//求数量
//         return 0; //求面积
   //      return 1; //求周长
    }
    
//     if(grid[m][n]==0){//求周长才有这条判断语句,其它没有
//         return 1;
//     }
    
    if(grid[m][n]!=1){
         return;//求数量
//         return 0; //求面积
     //   return 0;//求周长
    }
    


    grid[m][n]=2;
    
    //求数量
    dfs(grid,m-1,n);
    dfs(grid,m+1,n);
    dfs(grid,m,n-1);
    dfs(grid,m,n+1);
    
    //求面积
    //return 1+dfs(grid,m-1,n)+dfs(grid,m+1,n)+dfs(grid,m,n-1)+dfs(grid,m,n+1);
    
    //求周长
   // return dfs(grid,m-1,n)+dfs(grid,m+1,n)+dfs(grid,m,n-1)+dfs(grid,m,n+1);
    
}
function isArea(grid,m,n){
    return (0<=m&&m<grid.length&&0<=n&&n<grid[0].length)
}
module.exports = {
    solve : solve
};

编辑于 2021-11-22 18:48:33 回复(0)
/**
 * 判断岛屿数量
 * @param grid char字符型二维数组 
 * @return int整型
 */
function solve( grid ) {
    // write code here
    let rows = grid.length;
    let cols = grid[0].length;
    let res = 0;
    function dfs(i, j, tag=false) {
        if (i<0 || i>=rows || j<0 || j>=cols || grid[i][j] == 0) {
            return;
        }
        grid[i][j] = 0;
        dfs(i+1, j, true);
        dfs(i-1, j, true);
        dfs(i, j+1, true);
        dfs(i, j-1, true);
        if (tag === false) res++;
    }
    for(let i=0; i<rows; i++) {
        for (let j=0; j<cols; j++) {
            if (grid[i][j] == 1) {
                dfs(i, j);
            }
        }
    }
    return res;
}
module.exports = {
    solve : solve
};
JS用户表示很无语,测试用例里的好像不是数字,导致`===`匹配会出现问题
发表于 2021-08-04 21:07:25 回复(0)
dfs:
function solve( grid ) {
    function explore(i, j) {
        if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] !== '1') return;
        grid[i][j] = '0';
        explore(i - 1, j);
        explore(i + 1, j);
        explore(i, j - 1);
        explore(i, j + 1);
    }
    
    let count = 0;
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
            if (grid[i][j] !== '1') continue;
            count++;
            explore(i, j);
        }
    }
    return count;
}
bfs(超时了):
class Node {
    constructor(value = null) {
        this.value = value;
        this.next = null;
    }
}

class Queue {
    constructor() {
        this.head = new Node();
        this.tail = this.head;
    }
    add(value) {
        this.tail.next = new Node(value);
        this.tail = this.tail.next;
    }
    remove() {
        const node = this.head.next;
        this.head.next = node.next;
        node.next = null;
        if (this.head.next === null) this.tail = this.head;
        return node.value;
    }
    isEmpty() {
        return this.head === this.tail;
    }
}

/**
 * 判断岛屿数量
 * @param grid char字符型二维数组 
 * @return int整型
 */
function solve( grid ) {
    function explore(i, j) {
        const queue = new Queue();
        queue.add([i, j]);
        while (!queue.isEmpty()) {
            const [r, c] = queue.remove();
            grid[r][c] = 0;
            if (grid[r - 1] && grid[r - 1][c] === '1') queue.add([r - 1, c]);
            if (grid[r + 1] && grid[r + 1][c] === '1') queue.add([r + 1, c]);
            if (grid[r][c - 1] === '1') queue.add([r, c - 1]);
            if (grid[r][c + 1] === '1') queue.add([r, c + 1]);
        }
    }
    
    let count = 0;
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
            if (grid[i][j] !== '1') continue;
            count++;
            explore(i, j);
        }
    }
    return count;
}


编辑于 2021-04-08 16:53:50 回复(0)
深度优先遍历DFS 
function solve( grid ) {
    // write code here
   let res = 0
  for (let i = 0; i < grid.length; i++) {
    for (let j = 0; j < grid[0].length; j++) {
      if (grid[i][j] == 1) {
        res++
        dfs(i, j, grid)
      }
    }
  }
  return res
}
function dfs(i, j, grid) {
  if (i < 0 || i >= grid.length || j < 0  || j >= grid[0].length || grid[i][j] == 0 ) return;
  grid[i][j] = 0
  dfs(i, j + 1, grid)
  dfs(i, j - 1, grid)
  dfs(i + 1, j, grid)
  dfs(i - 1, j, grid)
}

编辑于 2021-01-20 16:34:28 回复(0)
为什么只能过93.75%
function solve( grid ) {
    // write code here
    //使用dfs  回溯
    if(grid == null || grid.length == 0) return 0
    let num=0;
    let m = grid.length;
    let n = grid[0].length;
    let count = (i,j)=> {
        grid[i][j] = 0;//标记访问过
        if(i+1<m && grid[i+1][j]==1) count(i+1,j);
        if(i-1>=0 && grid[i-1][j]==1) count(i-1,j);
        if(j+1<n && grid[i][j+1]==1) count(i,j+1);
        if(j-1>=0 && grid[i][j-1]==1) count(i,j-1);
    }
    for(let i=0; i<m; i++){
        for(let j=0; j<n ; j++) {
            if(grid[i][j] == 1){
                count(i,j);
                num++;
            }
        }
    }
    return num
}


发表于 2020-09-01 16:27:16 回复(8)