[LeetCode 地图分析] | 刷题打卡

254 阅读1分钟

1162. 地图分析

leetcode-cn.com/problems/as…

  • 单源bfs 超时
    • 每碰到一个海洋就向四周扩散 直到遇到陆地 同时更新最大距离
/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxDistance = function (grid) {
    const dirction = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    const width = grid.length
    const height = grid[0].length
    let maxDis = -1
    function bfs(i, j) {
        const visited = grid.map(item=>item.map(_=>false))
        const stack = [[i, j]]
        let counter = 0
        while (stack.length) {
            counter++
            console.log(stack)
            
            const size = stack.length
            for (let k = 0; k < size; k++) {
                const [x, y] = stack.shift()
                for (let dir = 0; dir < dirction.length; dir++) {
                    const nowX = x + dirction[dir][0]
                    const nowY = y + dirction[dir][1]
                    if (nowX >= 0 && nowY >= 0 && nowX < width && nowY < height && !visited[nowX][nowY]) {
                        if (grid[nowX][nowY] === 1) {
                            return counter
                        } else {
                            stack.push([nowX, nowY])
                            visited[nowX][nowY] = true
                        }

                    }
                }
            }
        }
        return -1
    }
    for (let i = 0; i < width; i++) {
        for (let j = 0; j < height; j++) {
            if (grid[i][j] === 0) {
                maxDis = Math.max(maxDis,bfs(i,j))
            }

        }
    }
    return maxDis
};
  • bfs 多源bfs 将所有的陆地放进队列中 然后同时扩散 最后扩散到的海洋就是最远的海洋
    • 1.将所有陆地放入队列
    • 2.扩散队列中的陆地,每遇到海洋入队同时标记已走过的海洋,防止死循环
/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxDistance = function (grid) {
    const dirction = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    const width = grid.length
    const height = grid[0].length
    
    const stack = []
    for (let i = 0; i < width; i++) {
        for (let j = 0; j < height; j++) {
            if (grid[i][j] === 1) {
                stack.push([i,j])
            }
        }
    }
    if(stack.length ===0 || stack.length === grid.length * grid[0].length){
        return -1
    }
    function bfs() {
        const visited = grid.map(item=>item.map(_=>false))
        let counter = -1
        while (stack.length) {
            counter++
            const size = stack.length
            for (let k = 0; k < size; k++) {
                const [x, y] = stack.shift()
                for (let dir = 0; dir < dirction.length; dir++) {
                    const nowX = x + dirction[dir][0]
                    const nowY = y + dirction[dir][1]
                    if (nowX >= 0 && nowY >= 0 && nowX < width && nowY < height && !visited[nowX][nowY]) {
                        if (grid[nowX][nowY] === 0) {
                            stack.push([nowX, nowY])
                            visited[nowX][nowY] = true
                        }

                    }
                }
            }
        }
        return counter
    }
    let maxDis = bfs()
    return  maxDis
};