From 072c14101852593191cb27b677c4cd80b2556ca7 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 21 Nov 2024 14:07:10 +0300 Subject: [PATCH 1/4] feat: add solutions to lc problem: No.2257 --- .../README.md | 111 ++++++++++++++++++ .../README_EN.md | 111 ++++++++++++++++++ .../Solution copy.js | 28 +++++ .../Solution.js | 28 +++++ .../Solution2.js | 29 +++++ .../Solution2.ts | 29 +++++ 6 files changed, 336 insertions(+) create mode 100644 solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js create mode 100644 solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.js create mode 100644 solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.js create mode 100644 solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.ts diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md index df85a041a3777..7bb02faf4a044 100644 --- a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md @@ -246,6 +246,117 @@ function countUnguarded(m: number, n: number, guards: number[][], walls: number[ } ``` +#### JavaScript + +```js +function countUnguarded(m, n, guards, walls) { + const g = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); + for (const [i, j] of guards) { + g[i][j] = 2; + } + for (const [i, j] of walls) { + g[i][j] = 2; + } + const dirs = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; ++k) { + let [x, y] = [i, j]; + let [a, b] = [dirs[k], dirs[k + 1]]; + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) { + x += a; + y += b; + g[x][y] = 1; + } + } + } + let ans = 0; + for (const row of g) { + for (const v of row) { + ans += v === 0 ? 1 : 0; + } + } + return ans; +} +``` + + + + + + + +### Solution 2: Simulation + + + +#### TypeScript + +```ts +function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number { + let c = 0; + const mtx = Array.from({ length: m }, () => Array(n).fill(0)); + for (const [i, j] of guards) mtx[i][j] = 2; + for (const [i, j] of walls) mtx[i][j] = 2; + + const dfs = (i: number, j: number, dx: number, dy: number) => { + [i, j] = [i + dx, j + dy]; + + if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return; + + if (mtx[i][j] === 0) { + mtx[i][j] = 1; + c++; + } + + dfs(i, j, dx, dy); + }; + + const DIRS = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; k++) { + const [dx, dy] = [DIRS[k], DIRS[k + 1]]; + dfs(i, j, dx, dy); + } + } + + return m * n - guards.length - walls.length - c; +} +``` + +#### JavaScript + +```js +function countUnguarded(m, n, guards, walls) { + let c = 0; + const mtx = Array.from({ length: m }, () => Array(n).fill(0)); + for (const [i, j] of guards) mtx[i][j] = 2; + for (const [i, j] of walls) mtx[i][j] = 2; + + const dfs = (i, j, dx, dy) => { + [i, j] = [i + dx, j + dy]; + + if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return; + + if (mtx[i][j] === 0) { + mtx[i][j] = 1; + c++; + } + + dfs(i, j, dx, dy); + }; + + const DIRS = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; k++) { + const [dx, dy] = [DIRS[k], DIRS[k + 1]]; + dfs(i, j, dx, dy); + } + } + + return m * n - guards.length - walls.length - c; +} +``` + diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md index d56f3851590cd..d751ca09c1e27 100644 --- a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md @@ -242,6 +242,117 @@ function countUnguarded(m: number, n: number, guards: number[][], walls: number[ } ``` +#### JavaScript + +```js +function countUnguarded(m, n, guards, walls) { + const g = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); + for (const [i, j] of guards) { + g[i][j] = 2; + } + for (const [i, j] of walls) { + g[i][j] = 2; + } + const dirs = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; ++k) { + let [x, y] = [i, j]; + let [a, b] = [dirs[k], dirs[k + 1]]; + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) { + x += a; + y += b; + g[x][y] = 1; + } + } + } + let ans = 0; + for (const row of g) { + for (const v of row) { + ans += v === 0 ? 1 : 0; + } + } + return ans; +} +``` + + + + + + + +### Solution 2: Simulation + + + +#### TypeScript + +```ts +function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number { + let c = 0; + const mtx = Array.from({ length: m }, () => Array(n).fill(0)); + for (const [i, j] of guards) mtx[i][j] = 2; + for (const [i, j] of walls) mtx[i][j] = 2; + + const dfs = (i: number, j: number, dx: number, dy: number) => { + [i, j] = [i + dx, j + dy]; + + if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return; + + if (mtx[i][j] === 0) { + mtx[i][j] = 1; + c++; + } + + dfs(i, j, dx, dy); + }; + + const DIRS = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; k++) { + const [dx, dy] = [DIRS[k], DIRS[k + 1]]; + dfs(i, j, dx, dy); + } + } + + return m * n - guards.length - walls.length - c; +} +``` + +#### JavaScript + +```js +function countUnguarded(m, n, guards, walls) { + let c = 0; + const mtx = Array.from({ length: m }, () => Array(n).fill(0)); + for (const [i, j] of guards) mtx[i][j] = 2; + for (const [i, j] of walls) mtx[i][j] = 2; + + const dfs = (i, j, dx, dy) => { + [i, j] = [i + dx, j + dy]; + + if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return; + + if (mtx[i][j] === 0) { + mtx[i][j] = 1; + c++; + } + + dfs(i, j, dx, dy); + }; + + const DIRS = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; k++) { + const [dx, dy] = [DIRS[k], DIRS[k + 1]]; + dfs(i, j, dx, dy); + } + } + + return m * n - guards.length - walls.length - c; +} +``` + diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js new file mode 100644 index 0000000000000..3e490b0577279 --- /dev/null +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js @@ -0,0 +1,28 @@ +function countUnguarded(m, n, guards, walls) { + const g = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); + for (const [i, j] of guards) { + g[i][j] = 2; + } + for (const [i, j] of walls) { + g[i][j] = 2; + } + const dirs = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; ++k) { + let [x, y] = [i, j]; + let [a, b] = [dirs[k], dirs[k + 1]]; + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) { + x += a; + y += b; + g[x][y] = 1; + } + } + } + let ans = 0; + for (const row of g) { + for (const v of row) { + ans += v === 0 ? 1 : 0; + } + } + return ans; +} diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.js b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.js new file mode 100644 index 0000000000000..3e490b0577279 --- /dev/null +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.js @@ -0,0 +1,28 @@ +function countUnguarded(m, n, guards, walls) { + const g = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); + for (const [i, j] of guards) { + g[i][j] = 2; + } + for (const [i, j] of walls) { + g[i][j] = 2; + } + const dirs = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; ++k) { + let [x, y] = [i, j]; + let [a, b] = [dirs[k], dirs[k + 1]]; + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) { + x += a; + y += b; + g[x][y] = 1; + } + } + } + let ans = 0; + for (const row of g) { + for (const v of row) { + ans += v === 0 ? 1 : 0; + } + } + return ans; +} diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.js b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.js new file mode 100644 index 0000000000000..0e9f67b15efb3 --- /dev/null +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.js @@ -0,0 +1,29 @@ +function countUnguarded(m, n, guards, walls) { + let c = 0; + const mtx = Array.from({ length: m }, () => Array(n).fill(0)); + for (const [i, j] of guards) mtx[i][j] = 2; + for (const [i, j] of walls) mtx[i][j] = 2; + + const dfs = (i, j, dx, dy) => { + [i, j] = [i + dx, j + dy]; + + if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return; + + if (mtx[i][j] === 0) { + mtx[i][j] = 1; + c++; + } + + dfs(i, j, dx, dy); + }; + + const DIRS = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; k++) { + const [dx, dy] = [DIRS[k], DIRS[k + 1]]; + dfs(i, j, dx, dy); + } + } + + return m * n - guards.length - walls.length - c; +} diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.ts b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.ts new file mode 100644 index 0000000000000..c1c4cfb766568 --- /dev/null +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution2.ts @@ -0,0 +1,29 @@ +function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number { + let c = 0; + const mtx = Array.from({ length: m }, () => Array(n).fill(0)); + for (const [i, j] of guards) mtx[i][j] = 2; + for (const [i, j] of walls) mtx[i][j] = 2; + + const dfs = (i: number, j: number, dx: number, dy: number) => { + [i, j] = [i + dx, j + dy]; + + if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return; + + if (mtx[i][j] === 0) { + mtx[i][j] = 1; + c++; + } + + dfs(i, j, dx, dy); + }; + + const DIRS = [-1, 0, 1, 0, -1]; + for (const [i, j] of guards) { + for (let k = 0; k < 4; k++) { + const [dx, dy] = [DIRS[k], DIRS[k + 1]]; + dfs(i, j, dx, dy); + } + } + + return m * n - guards.length - walls.length - c; +} From 08e6e8bcf52c69d89c5fd6c7e36c48b48d6c4a68 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 22 Nov 2024 15:33:21 +0800 Subject: [PATCH 2/4] Delete solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js --- .../Solution copy.js | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js deleted file mode 100644 index 3e490b0577279..0000000000000 --- a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution copy.js +++ /dev/null @@ -1,28 +0,0 @@ -function countUnguarded(m, n, guards, walls) { - const g = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); - for (const [i, j] of guards) { - g[i][j] = 2; - } - for (const [i, j] of walls) { - g[i][j] = 2; - } - const dirs = [-1, 0, 1, 0, -1]; - for (const [i, j] of guards) { - for (let k = 0; k < 4; ++k) { - let [x, y] = [i, j]; - let [a, b] = [dirs[k], dirs[k + 1]]; - while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) { - x += a; - y += b; - g[x][y] = 1; - } - } - } - let ans = 0; - for (const row of g) { - for (const v of row) { - ans += v === 0 ? 1 : 0; - } - } - return ans; -} From 82434ebbab62f7a4a1a40e4ee038be6a8d7d9674 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 22 Nov 2024 15:34:35 +0800 Subject: [PATCH 3/4] Update README.md --- .../2200-2299/2257.Count Unguarded Cells in the Grid/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md index 7bb02faf4a044..15f4ef1c6fa73 100644 --- a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md @@ -285,7 +285,7 @@ function countUnguarded(m, n, guards, walls) { -### Solution 2: Simulation +### 方法二:DFS + 模拟 From f0057997a5175caa3bf0bfab778389a3ddb999c7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 22 Nov 2024 15:34:52 +0800 Subject: [PATCH 4/4] Update README_EN.md --- .../2257.Count Unguarded Cells in the Grid/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md index d751ca09c1e27..b4e83d1ea1e33 100644 --- a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md @@ -281,7 +281,7 @@ function countUnguarded(m, n, guards, walls) { -### Solution 2: Simulation +### Solution 2: DFS + Simulation