islands

Given a two-dimensional grid by a '1' (land) and '0' (water) composition, the number of islands is calculated. An island surrounded by water, and it is through the horizontal or vertical direction is connected to each adjacent land. You can assume that the water surrounding the four sides of the grid are.

Example 1:

Enter:
11110
11010
11000
00000

Output: 1
Example 2:

Enter:
11000
11000
00100
00011

Output: 3

var numIslands = function(grid) {
    if(grid===null || grid.length===0) return 0;
   let c = grid.length;
   let k = grid[0].length;
    let step = 0;
    for(let i=0; i<c; i++){
        for(let j=0; j<k; j++){
            if(grid[i][j]==1){
                step++
                dfs(grid, i, j)
            }
        }
    }
    function dfs(grid, i, j){
        if(i<0 || i>=c || j<0 || j>=k || grid[i][j]==0){
            return;
        }
        // 最关键
        if(grid[i][j]==1) grid[i][j] = 0;
        dfs(grid, i+1, j)
        dfs(grid, i-1, j)
        dfs(grid, i, j+1)
        dfs(grid, i, j-1)
    }
    return step
};
/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function(grid) {
    if(grid===null || grid.length===0) return 0;
    let c = grid.length;
    let k = grid[0].length;
    let res = 0;
    let list = [];
    for(let i=0; i<c; i++){
        for(let j=0; j<k; j++){
            if(grid[i][j] == 1){
                res ++;
                grid[i][j] = 0;
                list.push([i, j])
               while(list.length){
                   let [i, j] = list.shift();
                    if(i-1>=0 && grid[i-1][j]==1) {
                        list.push([i-1, j]);
                        grid[i-1][j] = 0;                   
                    }
                   if(i+1<c && grid[i+1][j]==1) {
                        list.push([i+1, j]);
                        grid[i+1][j] = 0;                   
                    }
                   if(j-1>=0 && grid[i][j-1]==1) {
                        list.push([i, j-1]);
                        grid[i][j-1] = 0;                   
                    }
                   if(j+1<k && grid[i][j+1]==1) {
                        list.push([i, j+1]);
                        grid[i][j+1] = 0;                   
                    }
                }   
            }
        }
    }
    return res
};

 

There is a two-dimensional matrix  grid , each location either on land (to mark  0 ) is either water (for the sign  1 ).

We proceed from one land mass, each can up under the direction of the adjacent area of ​​about four to go, and went to all land areas, which we will call an "island."

If an island completely surrounded by water, that is, all the adjacent land areas around the edge of the waters are up and down, then we will call it "closed the island."

Please return the number of closed islands.

Example 1:

Input: grid = [[1,1,1,1,1,1,1,0], [1,0,0,0,0,1,1,0], [1,0,1,0, 1,1,1,0], [1,0,0,0,0,1,0,1], [1,1,1,1,1,1,1,0]] 
output: 2 
explanation: 
island island gray area is closed, because the island is completely surrounded by water (i.e., an area enclosed).

Example 2:

Input: grid = [[0,0,1,0,0], [0,1,0,1,0], [0,1,1,1,0]] 
Output: 1

Example 3:

Input: Grid = [[1,1,1,1,1,1,1], 
             [1,0,0,0,0,0,1], 
             [1,0,1,1,1,0, 1], 
             [1,0,1,0,1,0,1], 
             [1,0,1,1,1,0,1], 
             [1,0,0,0,0,0,1] , 
             [1,1,1,1,1,1,1]] 
output: 2

 

prompt:

  • 1 <= grid.length, grid[0].length <= 100
  • 0 <= grid[i][j] <=1

 

// This is what I wrote, infinite loop

const goto=(i, j, grid, flag)=>{
if(i<0 || j<0 || i>grid.length-1 || j>grid[0].length-1){
return;
}
if(grid[i][j]===1){
flag++;
return;
}

// 会在下面2个地方死循环
goto(i+1, j, grid, flag);
goto(i-1, j, grid, flag)
goto(i, j+1, grid, flag)
goto(i, j-1, grid, flag)
if(flag===4){
return true
}else{
return false
}
}
var closedIsland = function(grid) {
let count = 0, flag = 0;
for(let i=0; i<grid.length; i++){
let grid1 = grid[i];
for(let j=0; j<grid1.length; j++){
let item = grid1[j];
if(item === 0){
let res = goto(i, j, grid, flag);
if(res) count++
}
}
}
return count;
};

/**
 * @param {number[][]} grid
 * @return {number}
 */
const dx = [0, 1, 0, -1]
const dy = [1, 0, -1, 0]
var closedIsland = function(grid) {
    function bfs(x, y) {
        let flag = true
        const que = [[x, y]]
        while(que.length !== 0) {
            const [x, y] = que.shift()
            if (x === 0 || x === grid.length - 1 || y === 0 || y === grid[0].length - 1) flag = false
            for(let i = 0; i < 4; i++) {
                const xx = dx[i] + x
                const yy = dy[i] + y
                if (xx < 0 || xx >= grid.length) continue
                if (yy < 0 || yy >= grid[0].length) continue
                if (grid[xx][yy] === 0) {
                    que.push([xx, yy])
                    grid[xx][yy] = 1
                    if (xx === 0 || xx === grid.length - 1 || yy === 0 || yy === grid[0].length - 1) flag = false
                }
            }
        }
        // console.log(flag)
        if (flag) return 1
        return 0
    }
    let ans = 0
    for(let i = 0; i < grid.length; i++) {
        for(let j = 0; j < grid[i].length; j++) {
            if (grid[i][j] === 0) {
                ans += bfs(i, j)
                // console.log(i, j)
            }
        }
    }
    return ans
};
/**
 * @param {number[][]} grid
 * @return {number}
 */
var closedIsland = function(grid) {
    let result = 0;
    var dfs = function(x, y) {
        grid[x][y] = 1;
        let r = 1;
        if (x === 0) {
            r = 0;
        } else if (grid[x - 1][y] === 0 && dfs(x - 1, y) === 0) {
            r = 0;
        }
        if (y === 0) {
            r = 0;
        } else if (grid[x][y - 1] === 0 && dfs(x, y - 1) === 0) {
            r = 0;
        }
        if (x === grid.length - 1) {
            r = 0;
        } else if (grid[x + 1][y] === 0 && dfs(x + 1, y) === 0) {
            r = 0;
        }
        if (y === grid[0].length - 1) {
            r = 0;
        } else if (grid[x][y + 1] === 0 && dfs(x, y + 1) === 0) {
            r = 0;
        }
        return r;
    }
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[i].length; j++) {
            if (grid[i][j] === 0) {
                result += dfs(i, j);
            }
        }
    }
    return result;
};

 

 

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/number-of-islands
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/zhangzs000/p/11830885.html