[Javascript] Idiomatic BFS and DFS
Recursive DFS\n\nconst pacificAtlantic = (heights) => {\n    const m = heights.length, n = heights[0].length  \n    const atlantic = new Array(m).fill().map(() 
**Recursive DFS**\n```\nconst pacificAtlantic = (heights) => {\n    const m = heights.length, n = heights[0].length  \n    const atlantic = new Array(m).fill().map(() => new Array(n).fill(false))\n    const pacific = new Array(m).fill().map(() => new Array(n).fill(false))\n    const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]\n    const dfs = (x, y, visited) => {\n        visited[x][y] = true\n        for (let dir of directions) {\n            let nx = x + dir[0]\n            let ny = y + dir[1]\n            if (nx < 0 || ny < 0 || nx >= m || ny >= n || visited[nx][ny]) continue\n            if (heights[nx][ny] >= heights[x][y]) {\n                dfs(nx, ny, visited)\n            }\n        }\n    }\n    \n    for (let x = 0; x < m; x++) {\n        for (let y = 0; y < n; y++) {\n            if (x === 0 || y === 0) {\n                dfs(x, y, pacific)\n            }\n            if (x === m - 1 || y === n - 1) {\n                dfs(x, y, atlantic)\n            }\n        }\n    }\n    const paths = []\n    for (let x = 0; x < m; x++) {\n        for (let y = 0; y < n; y++) {\n            if (pacific[x][y] && atlantic[x][y]) {\n                paths.push([x, y])\n            }\n        }\n    }\n    return paths\n}\n```\n\n**BFS**\n\n```\nconst pacificAtlantic = (heights) => {\n    const m = heights.length, n = heights[0].length  \n    const atlanticQueue = []\n    const pacificQueue = []\n    \n    for (let x = 0; x < m; x++) {\n        for (let y = 0; y < n; y++) {\n            if (x === m - 1 || y === n - 1) {\n                atlanticQueue.push([x, y])\n            }\n            if (x === 0 || y === 0) {\n                pacificQueue.push([x, y])\n            }\n        }\n    } \n    const bfs = (queue) => {\n        const isValid = (x, y) => x >= 0 && y >= 0 && x < m && y < n\n        const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]\n        const visited = new Array(m).fill().map(() => new Array(n).fill(false))\n        while (queue.length) {\n            const [x, y] = queue.shift()\n            visited[x][y] = true\n            for (let dir of directions) {\n                let nx = x + dir[0]\n                let ny = y + dir[1]\n                if (!isValid(nx, ny) || visited[nx][ny]) continue\n                if (heights[nx][ny] >= heights[x][y]) {\n                    queue.push([nx, ny])\n                }\n            }\n        }\n        return visited\n    }\n    const pacific = bfs(atlanticQueue)\n    const atlantic = bfs(pacificQueue)\n    const paths = []\n    for (let x = 0; x < m; x++) {\n        for (let y = 0; y < n; y++) {\n            if (pacific[x][y] && atlantic[x][y]) {\n                paths.push([x, y])\n            }\n        }\n    }\n    return paths\n}\n```
['Depth-First Search', 'Breadth-First Search', 'JavaScript']