Skip to content

Commit 5ffcc4b

Browse files
committed
feat: add solutions to lc problem: No.2257
No.2257.Count Unguarded Cells in the Grid
1 parent 37bc5c5 commit 5ffcc4b

File tree

6 files changed

+168
-165
lines changed

6 files changed

+168
-165
lines changed

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

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:模拟**
57+
58+
我们创建一个 $m \times n$ 的二维数组 $g$,其中 $g[i][j]$ 表示第 $i$ 行第 $j$ 列的格子。初始时,$g[i][j]$ 的值为 $0$,表示该格子没有被保卫。
59+
60+
然后遍历所有的警卫和墙,将 $g[i][j]$ 的值置为 $2$,这些位置不能被访问。
61+
62+
接下来,我们遍历所有警卫的位置,从该位置出发,向四个方向进行模拟,直到遇到墙或警卫,或者越界。在模拟的过程中,将遇到的格子的值置为 $1$,表示该格子被保卫。
63+
64+
最后,我们遍历 $g$,统计值为 $0$ 的格子的个数,即为答案。
65+
66+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为网格的行数和列数。
67+
5668
<!-- tabs:start -->
5769

5870
### **Python3**
@@ -64,23 +76,19 @@ class Solution:
6476
def countUnguarded(
6577
self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]
6678
) -> int:
67-
g = [[None] * n for _ in range(m)]
68-
for r, c in guards:
69-
g[r][c] = 'g'
70-
for r, c in walls:
71-
g[r][c] = 'w'
79+
g = [[0] * n for _ in range(m)]
7280
for i, j in guards:
73-
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
81+
g[i][j] = 2
82+
for i, j in walls:
83+
g[i][j] = 2
84+
dirs = (-1, 0, 1, 0, -1)
85+
for i, j in guards:
86+
for a, b in pairwise(dirs):
7487
x, y = i, j
75-
while (
76-
0 <= x + a < m
77-
and 0 <= y + b < n
78-
and g[x + a][y + b] != 'w'
79-
and g[x + a][y + b] != 'g'
80-
):
88+
while 0 <= x + a < m and 0 <= y + b < n and g[x + a][y + b] < 2:
8189
x, y = x + a, y + b
82-
g[x][y] = 'v'
83-
return sum(not v for row in g for v in row)
90+
g[x][y] = 1
91+
return sum(v == 0 for row in g for v in row)
8492
```
8593

8694
### **Java**
@@ -90,31 +98,28 @@ class Solution:
9098
```java
9199
class Solution {
92100
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
93-
char[][] g = new char[m][n];
94-
for (int[] e : guards) {
95-
int r = e[0], c = e[1];
96-
g[r][c] = 'g';
101+
int[][] g = new int[m][n];
102+
for (var e : guards) {
103+
g[e[0]][e[1]] = 2;
97104
}
98-
for (int[] e : walls) {
99-
int r = e[0], c = e[1];
100-
g[r][c] = 'w';
105+
for (var e : walls) {
106+
g[e[0]][e[1]] = 2;
101107
}
102-
int[][] dirs = new int[][] {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
103-
for (int[] p : guards) {
104-
for (int[] dir : dirs) {
105-
int a = dir[0], b = dir[1];
106-
int x = p[0], y = p[1];
107-
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w'
108-
&& g[x + a][y + b] != 'g') {
108+
int[] dirs = {-1, 0, 1, 0, -1};
109+
for (var e : guards) {
110+
for (int k = 0; k < 4; ++k) {
111+
int x = e[0], y = e[1];
112+
int a = dirs[k], b = dirs[k + 1];
113+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) {
109114
x += a;
110115
y += b;
111-
g[x][y] = 'v';
116+
g[x][y] = 1;
112117
}
113118
}
114119
}
115120
int ans = 0;
116-
for (char[] row : g) {
117-
for (char v : row) {
121+
for (var row : g) {
122+
for (int v : row) {
118123
if (v == 0) {
119124
++ans;
120125
}
@@ -131,25 +136,30 @@ class Solution {
131136
class Solution {
132137
public:
133138
int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
134-
vector<vector<char>> g(m, vector<char>(n));
135-
for (auto& e : guards) g[e[0]][e[1]] = 'g';
136-
for (auto& e : walls) g[e[0]][e[1]] = 'w';
137-
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
138-
for (auto& p : guards) {
139-
for (auto& dir : dirs) {
140-
int a = dir[0], b = dir[1];
141-
int x = p[0], y = p[1];
142-
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w' && g[x + a][y + b] != 'g') {
139+
int g[m][n];
140+
memset(g, 0, sizeof(g));
141+
for (auto& e : guards) {
142+
g[e[0]][e[1]] = 2;
143+
}
144+
for (auto& e : walls) {
145+
g[e[0]][e[1]] = 2;
146+
}
147+
int dirs[5] = {-1, 0, 1, 0, -1};
148+
for (auto& e : guards) {
149+
for (int k = 0; k < 4; ++k) {
150+
int x = e[0], y = e[1];
151+
int a = dirs[k], b = dirs[k + 1];
152+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) {
143153
x += a;
144154
y += b;
145-
g[x][y] = 'v';
155+
g[x][y] = 1;
146156
}
147157
}
148158
}
149159
int ans = 0;
150-
for (auto& row : g)
151-
for (auto& v : row)
152-
ans += v == 0;
160+
for (auto& row : g) {
161+
ans += count(row, row + n, 0);
162+
}
153163
return ans;
154164
}
155165
};
@@ -158,37 +168,36 @@ public:
158168
### **Go**
159169
160170
```go
161-
func countUnguarded(m int, n int, guards [][]int, walls [][]int) int {
171+
func countUnguarded(m int, n int, guards [][]int, walls [][]int) (ans int) {
162172
g := make([][]int, m)
163173
for i := range g {
164174
g[i] = make([]int, n)
165175
}
166176
for _, e := range guards {
167-
g[e[0]][e[1]] = 1
177+
g[e[0]][e[1]] = 2
168178
}
169179
for _, e := range walls {
170180
g[e[0]][e[1]] = 2
171181
}
172-
dirs := [][]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
173-
for _, p := range guards {
174-
for _, dir := range dirs {
175-
a, b := dir[0], dir[1]
176-
x, y := p[0], p[1]
177-
for x+a >= 0 && x+a < m && y+b >= 0 && y+b < n && g[x+a][y+b] != 1 && g[x+a][y+b] != 2 {
182+
dirs := [5]int{-1, 0, 1, 0, -1}
183+
for _, e := range guards {
184+
for k := 0; k < 4; k++ {
185+
x, y := e[0], e[1]
186+
a, b := dirs[k], dirs[k+1]
187+
for x+a >= 0 && x+a < m && y+b >= 0 && y+b < n && g[x+a][y+b] < 2 {
178188
x, y = x+a, y+b
179-
g[x][y] = 3
189+
g[x][y] = 1
180190
}
181191
}
182192
}
183-
ans := 0
184193
for _, row := range g {
185194
for _, v := range row {
186195
if v == 0 {
187196
ans++
188197
}
189198
}
190199
}
191-
return ans
200+
return
192201
}
193202
```
194203

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

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,55 +54,48 @@ class Solution:
5454
def countUnguarded(
5555
self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]
5656
) -> int:
57-
g = [[None] * n for _ in range(m)]
58-
for r, c in guards:
59-
g[r][c] = 'g'
60-
for r, c in walls:
61-
g[r][c] = 'w'
57+
g = [[0] * n for _ in range(m)]
6258
for i, j in guards:
63-
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
59+
g[i][j] = 2
60+
for i, j in walls:
61+
g[i][j] = 2
62+
dirs = (-1, 0, 1, 0, -1)
63+
for i, j in guards:
64+
for a, b in pairwise(dirs):
6465
x, y = i, j
65-
while (
66-
0 <= x + a < m
67-
and 0 <= y + b < n
68-
and g[x + a][y + b] != 'w'
69-
and g[x + a][y + b] != 'g'
70-
):
66+
while 0 <= x + a < m and 0 <= y + b < n and g[x + a][y + b] < 2:
7167
x, y = x + a, y + b
72-
g[x][y] = 'v'
73-
return sum(not v for row in g for v in row)
68+
g[x][y] = 1
69+
return sum(v == 0 for row in g for v in row)
7470
```
7571

7672
### **Java**
7773

7874
```java
7975
class Solution {
8076
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
81-
char[][] g = new char[m][n];
82-
for (int[] e : guards) {
83-
int r = e[0], c = e[1];
84-
g[r][c] = 'g';
77+
int[][] g = new int[m][n];
78+
for (var e : guards) {
79+
g[e[0]][e[1]] = 2;
8580
}
86-
for (int[] e : walls) {
87-
int r = e[0], c = e[1];
88-
g[r][c] = 'w';
81+
for (var e : walls) {
82+
g[e[0]][e[1]] = 2;
8983
}
90-
int[][] dirs = new int[][] {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
91-
for (int[] p : guards) {
92-
for (int[] dir : dirs) {
93-
int a = dir[0], b = dir[1];
94-
int x = p[0], y = p[1];
95-
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w'
96-
&& g[x + a][y + b] != 'g') {
84+
int[] dirs = {-1, 0, 1, 0, -1};
85+
for (var e : guards) {
86+
for (int k = 0; k < 4; ++k) {
87+
int x = e[0], y = e[1];
88+
int a = dirs[k], b = dirs[k + 1];
89+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) {
9790
x += a;
9891
y += b;
99-
g[x][y] = 'v';
92+
g[x][y] = 1;
10093
}
10194
}
10295
}
10396
int ans = 0;
104-
for (char[] row : g) {
105-
for (char v : row) {
97+
for (var row : g) {
98+
for (int v : row) {
10699
if (v == 0) {
107100
++ans;
108101
}
@@ -119,25 +112,30 @@ class Solution {
119112
class Solution {
120113
public:
121114
int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
122-
vector<vector<char>> g(m, vector<char>(n));
123-
for (auto& e : guards) g[e[0]][e[1]] = 'g';
124-
for (auto& e : walls) g[e[0]][e[1]] = 'w';
125-
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
126-
for (auto& p : guards) {
127-
for (auto& dir : dirs) {
128-
int a = dir[0], b = dir[1];
129-
int x = p[0], y = p[1];
130-
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w' && g[x + a][y + b] != 'g') {
115+
int g[m][n];
116+
memset(g, 0, sizeof(g));
117+
for (auto& e : guards) {
118+
g[e[0]][e[1]] = 2;
119+
}
120+
for (auto& e : walls) {
121+
g[e[0]][e[1]] = 2;
122+
}
123+
int dirs[5] = {-1, 0, 1, 0, -1};
124+
for (auto& e : guards) {
125+
for (int k = 0; k < 4; ++k) {
126+
int x = e[0], y = e[1];
127+
int a = dirs[k], b = dirs[k + 1];
128+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) {
131129
x += a;
132130
y += b;
133-
g[x][y] = 'v';
131+
g[x][y] = 1;
134132
}
135133
}
136134
}
137135
int ans = 0;
138-
for (auto& row : g)
139-
for (auto& v : row)
140-
ans += v == 0;
136+
for (auto& row : g) {
137+
ans += count(row, row + n, 0);
138+
}
141139
return ans;
142140
}
143141
};
@@ -146,37 +144,36 @@ public:
146144
### **Go**
147145
148146
```go
149-
func countUnguarded(m int, n int, guards [][]int, walls [][]int) int {
147+
func countUnguarded(m int, n int, guards [][]int, walls [][]int) (ans int) {
150148
g := make([][]int, m)
151149
for i := range g {
152150
g[i] = make([]int, n)
153151
}
154152
for _, e := range guards {
155-
g[e[0]][e[1]] = 1
153+
g[e[0]][e[1]] = 2
156154
}
157155
for _, e := range walls {
158156
g[e[0]][e[1]] = 2
159157
}
160-
dirs := [][]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
161-
for _, p := range guards {
162-
for _, dir := range dirs {
163-
a, b := dir[0], dir[1]
164-
x, y := p[0], p[1]
165-
for x+a >= 0 && x+a < m && y+b >= 0 && y+b < n && g[x+a][y+b] != 1 && g[x+a][y+b] != 2 {
158+
dirs := [5]int{-1, 0, 1, 0, -1}
159+
for _, e := range guards {
160+
for k := 0; k < 4; k++ {
161+
x, y := e[0], e[1]
162+
a, b := dirs[k], dirs[k+1]
163+
for x+a >= 0 && x+a < m && y+b >= 0 && y+b < n && g[x+a][y+b] < 2 {
166164
x, y = x+a, y+b
167-
g[x][y] = 3
165+
g[x][y] = 1
168166
}
169167
}
170168
}
171-
ans := 0
172169
for _, row := range g {
173170
for _, v := range row {
174171
if v == 0 {
175172
ans++
176173
}
177174
}
178175
}
179-
return ans
176+
return
180177
}
181178
```
182179

0 commit comments

Comments
 (0)