update 130.被围绕的区域,417.太平洋大西洋水流问题新增 JS 广搜和深搜解法

This commit is contained in:
Relsola
2024-01-03 20:31:05 +08:00
parent 4b0226cf2c
commit d479e755cb
2 changed files with 237 additions and 0 deletions

View File

@ -435,6 +435,132 @@ class Solution:
``` ```
### JavaScript
```JavaScript
/**
* @description 深度搜索优先
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
function solve(board) {
const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]];
const [rowSize, colSize] = [board.length, board[0].length];
function dfs(board, x, y) {
board[x][y] = 'A';
for (let i = 0; i < 4; i++) {
const nextX = dir[i][0] + x;
const nextY = dir[i][1] + y;
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
continue;
}
if (board[nextX][nextY] === 'O') {
dfs(board, nextX, nextY);
}
}
}
for (let i = 0; i < rowSize; i++) {
if (board[i][0] === 'O') {
dfs(board, i, 0);
}
if (board[i][colSize - 1] === 'O') {
dfs(board, i, colSize - 1);
}
}
for (let i = 1; i < colSize - 1; i++) {
if (board[0][i] === 'O') {
dfs(board, 0, i);
}
if (board[rowSize - 1][i] === 'O') {
dfs(board, rowSize - 1, i);
}
}
for (let i = 0; i < rowSize; i++) {
for (let k = 0; k < colSize; k++) {
if (board[i][k] === 'A') {
board[i][k] = 'O';
} else if (board[i][k] === 'O') {
board[i][k] = 'X';
}
}
}
}
/**
* @description 广度搜索优先
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
function solve(board) {
const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]];
const [rowSize, colSize] = [board.length, board[0].length];
function bfs(board, x, y) {
board[x][y] = 'A';
const stack = [y, x];
while (stack.length !== 0) {
const top = [stack.pop(), stack.pop()];
for (let i = 0; i < 4; i++) {
const nextX = dir[i][0] + top[0];
const nextY = dir[i][1] + top[1];
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
continue;
}
if (board[nextX][nextY] === 'O') {
board[nextX][nextY] = 'A';
stack.push(nextY, nextX);
}
}
}
for (let i = 0; i < 4; i++) {
const nextX = dir[i][0] + x;
const nextY = dir[i][1] + y;
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
continue;
}
if (board[nextX][nextY] === 'O') {
dfs(board, nextX, nextY);
}
}
}
for (let i = 0; i < rowSize; i++) {
if (board[i][0] === 'O') {
bfs(board, i, 0);
}
if (board[i][colSize - 1] === 'O') {
bfs(board, i, colSize - 1);
}
}
for (let i = 1; i < colSize - 1; i++) {
if (board[0][i] === 'O') {
bfs(board, 0, i);
}
if (board[rowSize - 1][i] === 'O') {
bfs(board, rowSize - 1, i);
}
}
for (let i = 0; i < rowSize; i++) {
for (let k = 0; k < colSize; k++) {
if (board[i][k] === 'A') {
board[i][k] = 'O';
} else if (board[i][k] === 'O') {
board[i][k] = 'X';
}
}
}
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -450,6 +450,117 @@ class Solution:
return ans return ans
``` ```
### JavaScript
```JavaScript
/**
* @description 深度搜索优先
* @param {number[][]} heights
* @return {number[][]}
*/
function pacificAtlantic(heights) {
const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]];
const [rowSize, colSize] = [heights.length, heights[0].length];
const visited = Array.from({ length: rowSize }, _ =>
Array.from({ length: colSize }, _ => new Array(2).fill(false))
);
const result = [];
function dfs(heights, visited, x, y, sign) {
if (visited[x][y][sign]) {
return;
}
visited[x][y][sign] = true;
for (let i = 0; i < 4; i++) {
const nextX = x + dir[i][0];
const nextY = y + dir[i][1];
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
continue;
}
if (heights[x][y] > heights[nextX][nextY]) {
continue;
}
dfs(heights, visited, nextX, nextY, sign);
}
}
for (let i = 0; i < rowSize; i++) {
dfs(heights, visited, i, 0, 0);
dfs(heights, visited, i, colSize - 1, 1);
}
for (let i = 0; i < colSize; i++) {
dfs(heights, visited, 0, i, 0);
dfs(heights, visited, rowSize - 1, i, 1);
}
for (let i = 0; i < rowSize; i++) {
for (let k = 0; k < colSize; k++) {
if (visited[i][k][0] && visited[i][k][1]) {
result.push([i, k]);
}
}
}
return result;
}
/**
* @description 广度搜索优先
* @param {number[][]} heights
* @return {number[][]}
*/
function pacificAtlantic(heights) {
const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]];
const [rowSize, colSize] = [heights.length, heights[0].length];
const visited = Array.from({ length: rowSize }, _ =>
Array.from({ length: colSize }, _ => new Array(2).fill(false))
);
const result = [];
function bfs(heights, visited, x, y, sign) {
if (visited[x][y][sign]) {
return;
}
visited[x][y][sign] = true;
const stack = [y, x];
while (stack.length !== 0) {
[x, y] = [stack.pop(), stack.pop()];
for (let i = 0; i < 4; i++) {
const nextX = x + dir[i][0];
const nextY = y + dir[i][1];
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
continue;
}
if (heights[x][y] > heights[nextX][nextY] || visited[nextX][nextY][sign]) {
continue;
}
visited[nextX][nextY][sign] = true;
stack.push(nextY, nextX);
}
}
}
for (let i = 0; i < rowSize; i++) {
bfs(heights, visited, i, 0, 0);
bfs(heights, visited, i, colSize - 1, 1);
}
for (let i = 0; i < colSize; i++) {
bfs(heights, visited, 0, i, 0);
bfs(heights, visited, rowSize - 1, i, 1);
}
for (let i = 0; i < rowSize; i++) {
for (let k = 0; k < colSize; k++) {
if (visited[i][k][0] && visited[i][k][1]) {
result.push([i, k]);
}
}
}
return result;
}
```