Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problem: No.2257 #3798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 4 commits into doocs:main from rain84:feature/lc-2257
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:DFS + 模拟

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
111 changes: 111 additions & 0 deletions solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: DFS + Simulation

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

AltStyle によって変換されたページ (->オリジナル) /