Skip to content

Commit c67c40e

Browse files
committed
feat: add solutions to lc problem: No.2257
1 parent f07f5be commit c67c40e

File tree

6 files changed

+213
-188
lines changed

6 files changed

+213
-188
lines changed

solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -279,81 +279,86 @@ function countUnguarded(m, n, guards, walls) {
279279
}
280280
```
281281

282-
<!-- tabs:end -->
283-
284-
<!-- solution:end -->
285-
286-
<!-- solution:start -->
287-
288-
### 方法二:DFS + 模拟
289-
290-
<!-- tabs:start -->
291-
292-
#### TypeScript
293-
294-
```ts
295-
function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number {
296-
let c = 0;
297-
const mtx = Array.from({ length: m }, () => Array(n).fill(0));
298-
for (const [i, j] of guards) mtx[i][j] = 2;
299-
for (const [i, j] of walls) mtx[i][j] = 2;
300-
301-
const dfs = (i: number, j: number, dx: number, dy: number) => {
302-
[i, j] = [i + dx, j + dy];
303-
304-
if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return;
305-
306-
if (mtx[i][j] === 0) {
307-
mtx[i][j] = 1;
308-
c++;
282+
#### Rust
283+
284+
```rust
285+
impl Solution {
286+
pub fn count_unguarded(m: i32, n: i32, guards: Vec<Vec<i32>>, walls: Vec<Vec<i32>>) -> i32 {
287+
let m = m as usize;
288+
let n = n as usize;
289+
let mut g = vec![vec![0; n]; m];
290+
for e in &guards {
291+
g[e[0] as usize][e[1] as usize] = 2;
309292
}
310-
311-
dfs(i, j, dx, dy);
312-
};
313-
314-
const DIRS = [-1, 0, 1, 0, -1];
315-
for (const [i, j] of guards) {
316-
for (let k = 0; k < 4; k++) {
317-
const [dx, dy] = [DIRS[k], DIRS[k + 1]];
318-
dfs(i, j, dx, dy);
293+
for e in &walls {
294+
g[e[0] as usize][e[1] as usize] = 2;
319295
}
296+
let dirs = [-1, 0, 1, 0, -1];
297+
for e in &guards {
298+
let (x0, y0) = (e[0] as i32, e[1] as i32);
299+
for k in 0..4 {
300+
let (mut x, mut y) = (x0, y0);
301+
let (a, b) = (dirs[k], dirs[k + 1]);
302+
while x + a >= 0
303+
&& x + a < m as i32
304+
&& y + b >= 0
305+
&& y + b < n as i32
306+
&& g[(x + a) as usize][(y + b) as usize] < 2
307+
{
308+
x += a;
309+
y += b;
310+
g[x as usize][y as usize] = 1;
311+
}
312+
}
313+
}
314+
let mut ans = 0;
315+
for row in g {
316+
for v in row {
317+
if v == 0 {
318+
ans += 1;
319+
}
320+
}
321+
}
322+
ans
320323
}
321-
322-
return m * n - guards.length - walls.length - c;
323324
}
324325
```
325326

326-
#### JavaScript
327-
328-
```js
329-
function countUnguarded(m, n, guards, walls) {
330-
let c = 0;
331-
const mtx = Array.from({ length: m }, () => Array(n).fill(0));
332-
for (const [i, j] of guards) mtx[i][j] = 2;
333-
for (const [i, j] of walls) mtx[i][j] = 2;
334-
335-
const dfs = (i, j, dx, dy) => {
336-
[i, j] = [i + dx, j + dy];
327+
#### C#
337328

338-
if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return;
339-
340-
if (mtx[i][j] === 0) {
341-
mtx[i][j] = 1;
342-
c++;
329+
```cs
330+
public class Solution {
331+
public int CountUnguarded(int m, int n, int[][] guards, int[][] walls) {
332+
int[,] g = new int[m, n];
333+
foreach (var e in guards) {
334+
g[e[0], e[1]] = 2;
343335
}
344-
345-
dfs(i, j, dx, dy);
346-
};
347-
348-
const DIRS = [-1, 0, 1, 0, -1];
349-
for (const [i, j] of guards) {
350-
for (let k = 0; k < 4; k++) {
351-
const [dx, dy] = [DIRS[k], DIRS[k + 1]];
352-
dfs(i, j, dx, dy);
336+
foreach (var e in walls) {
337+
g[e[0], e[1]] = 2;
353338
}
339+
int[] dirs = { -1, 0, 1, 0, -1 };
340+
foreach (var e in guards) {
341+
int x0 = e[0], y0 = e[1];
342+
for (int k = 0; k < 4; ++k) {
343+
int x = x0, y = y0;
344+
int a = dirs[k], b = dirs[k + 1];
345+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a, y + b] < 2) {
346+
x += a;
347+
y += b;
348+
g[x, y] = 1;
349+
}
350+
}
351+
}
352+
int ans = 0;
353+
for (int i = 0; i < m; ++i) {
354+
for (int j = 0; j < n; ++j) {
355+
if (g[i, j] == 0) {
356+
++ans;
357+
}
358+
}
359+
}
360+
return ans;
354361
}
355-
356-
return m * n - guards.length - walls.length - c;
357362
}
358363
```
359364

solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -275,81 +275,86 @@ function countUnguarded(m, n, guards, walls) {
275275
}
276276
```
277277

278-
<!-- tabs:end -->
279-
280-
<!-- solution:end -->
281-
282-
<!-- solution:start -->
283-
284-
### Solution 2: DFS + Simulation
285-
286-
<!-- tabs:start -->
287-
288-
#### TypeScript
289-
290-
```ts
291-
function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number {
292-
let c = 0;
293-
const mtx = Array.from({ length: m }, () => Array(n).fill(0));
294-
for (const [i, j] of guards) mtx[i][j] = 2;
295-
for (const [i, j] of walls) mtx[i][j] = 2;
296-
297-
const dfs = (i: number, j: number, dx: number, dy: number) => {
298-
[i, j] = [i + dx, j + dy];
299-
300-
if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return;
301-
302-
if (mtx[i][j] === 0) {
303-
mtx[i][j] = 1;
304-
c++;
278+
#### Rust
279+
280+
```rust
281+
impl Solution {
282+
pub fn count_unguarded(m: i32, n: i32, guards: Vec<Vec<i32>>, walls: Vec<Vec<i32>>) -> i32 {
283+
let m = m as usize;
284+
let n = n as usize;
285+
let mut g = vec![vec![0; n]; m];
286+
for e in &guards {
287+
g[e[0] as usize][e[1] as usize] = 2;
305288
}
306-
307-
dfs(i, j, dx, dy);
308-
};
309-
310-
const DIRS = [-1, 0, 1, 0, -1];
311-
for (const [i, j] of guards) {
312-
for (let k = 0; k < 4; k++) {
313-
const [dx, dy] = [DIRS[k], DIRS[k + 1]];
314-
dfs(i, j, dx, dy);
289+
for e in &walls {
290+
g[e[0] as usize][e[1] as usize] = 2;
315291
}
292+
let dirs = [-1, 0, 1, 0, -1];
293+
for e in &guards {
294+
let (x0, y0) = (e[0] as i32, e[1] as i32);
295+
for k in 0..4 {
296+
let (mut x, mut y) = (x0, y0);
297+
let (a, b) = (dirs[k], dirs[k + 1]);
298+
while x + a >= 0
299+
&& x + a < m as i32
300+
&& y + b >= 0
301+
&& y + b < n as i32
302+
&& g[(x + a) as usize][(y + b) as usize] < 2
303+
{
304+
x += a;
305+
y += b;
306+
g[x as usize][y as usize] = 1;
307+
}
308+
}
309+
}
310+
let mut ans = 0;
311+
for row in g {
312+
for v in row {
313+
if v == 0 {
314+
ans += 1;
315+
}
316+
}
317+
}
318+
ans
316319
}
317-
318-
return m * n - guards.length - walls.length - c;
319320
}
320321
```
321322

322-
#### JavaScript
323-
324-
```js
325-
function countUnguarded(m, n, guards, walls) {
326-
let c = 0;
327-
const mtx = Array.from({ length: m }, () => Array(n).fill(0));
328-
for (const [i, j] of guards) mtx[i][j] = 2;
329-
for (const [i, j] of walls) mtx[i][j] = 2;
330-
331-
const dfs = (i, j, dx, dy) => {
332-
[i, j] = [i + dx, j + dy];
323+
#### C#
333324

334-
if (i < 0 || m <= i || j < 0 || n <= j || mtx[i][j] === 2) return;
335-
336-
if (mtx[i][j] === 0) {
337-
mtx[i][j] = 1;
338-
c++;
325+
```cs
326+
public class Solution {
327+
public int CountUnguarded(int m, int n, int[][] guards, int[][] walls) {
328+
int[,] g = new int[m, n];
329+
foreach (var e in guards) {
330+
g[e[0], e[1]] = 2;
339331
}
340-
341-
dfs(i, j, dx, dy);
342-
};
343-
344-
const DIRS = [-1, 0, 1, 0, -1];
345-
for (const [i, j] of guards) {
346-
for (let k = 0; k < 4; k++) {
347-
const [dx, dy] = [DIRS[k], DIRS[k + 1]];
348-
dfs(i, j, dx, dy);
332+
foreach (var e in walls) {
333+
g[e[0], e[1]] = 2;
349334
}
335+
int[] dirs = { -1, 0, 1, 0, -1 };
336+
foreach (var e in guards) {
337+
int x0 = e[0], y0 = e[1];
338+
for (int k = 0; k < 4; ++k) {
339+
int x = x0, y = y0;
340+
int a = dirs[k], b = dirs[k + 1];
341+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a, y + b] < 2) {
342+
x += a;
343+
y += b;
344+
g[x, y] = 1;
345+
}
346+
}
347+
}
348+
int ans = 0;
349+
for (int i = 0; i < m; ++i) {
350+
for (int j = 0; j < n; ++j) {
351+
if (g[i, j] == 0) {
352+
++ans;
353+
}
354+
}
355+
}
356+
return ans;
350357
}
351-
352-
return m * n - guards.length - walls.length - c;
353358
}
354359
```
355360

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public int CountUnguarded(int m, int n, int[][] guards, int[][] walls) {
3+
int[,] g = new int[m, n];
4+
foreach (var e in guards) {
5+
g[e[0], e[1]] = 2;
6+
}
7+
foreach (var e in walls) {
8+
g[e[0], e[1]] = 2;
9+
}
10+
int[] dirs = { -1, 0, 1, 0, -1 };
11+
foreach (var e in guards) {
12+
int x0 = e[0], y0 = e[1];
13+
for (int k = 0; k < 4; ++k) {
14+
int x = x0, y = y0;
15+
int a = dirs[k], b = dirs[k + 1];
16+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a, y + b] < 2) {
17+
x += a;
18+
y += b;
19+
g[x, y] = 1;
20+
}
21+
}
22+
}
23+
int ans = 0;
24+
for (int i = 0; i < m; ++i) {
25+
for (int j = 0; j < n; ++j) {
26+
if (g[i, j] == 0) {
27+
++ans;
28+
}
29+
}
30+
}
31+
return ans;
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
impl Solution {
2+
pub fn count_unguarded(m: i32, n: i32, guards: Vec<Vec<i32>>, walls: Vec<Vec<i32>>) -> i32 {
3+
let m = m as usize;
4+
let n = n as usize;
5+
let mut g = vec![vec![0; n]; m];
6+
for e in &guards {
7+
g[e[0] as usize][e[1] as usize] = 2;
8+
}
9+
for e in &walls {
10+
g[e[0] as usize][e[1] as usize] = 2;
11+
}
12+
let dirs = [-1, 0, 1, 0, -1];
13+
for e in &guards {
14+
let (x0, y0) = (e[0] as i32, e[1] as i32);
15+
for k in 0..4 {
16+
let (mut x, mut y) = (x0, y0);
17+
let (a, b) = (dirs[k], dirs[k + 1]);
18+
while x + a >= 0
19+
&& x + a < m as i32
20+
&& y + b >= 0
21+
&& y + b < n as i32
22+
&& g[(x + a) as usize][(y + b) as usize] < 2
23+
{
24+
x += a;
25+
y += b;
26+
g[x as usize][y as usize] = 1;
27+
}
28+
}
29+
}
30+
let mut ans = 0;
31+
for row in g {
32+
for v in row {
33+
if v == 0 {
34+
ans += 1;
35+
}
36+
}
37+
}
38+
ans
39+
}
40+
}

0 commit comments

Comments
 (0)