@@ -242,6 +242,117 @@ function countUnguarded(m: number, n: number, guards: number[][], walls: number[
242
242
}
243
243
```
244
244
245
+ #### JavaScript
246
+
247
+ ``` js
248
+ function countUnguarded (m , n , guards , walls ) {
249
+ const g = Array .from ({ length: m }, () => Array .from ({ length: n }, () => 0 ));
250
+ for (const [i , j ] of guards) {
251
+ g[i][j] = 2 ;
252
+ }
253
+ for (const [i , j ] of walls) {
254
+ g[i][j] = 2 ;
255
+ }
256
+ const dirs = [- 1 , 0 , 1 , 0 , - 1 ];
257
+ for (const [i , j ] of guards) {
258
+ for (let k = 0 ; k < 4 ; ++ k) {
259
+ let [x, y] = [i, j];
260
+ let [a, b] = [dirs[k], dirs[k + 1 ]];
261
+ while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2 ) {
262
+ x += a;
263
+ y += b;
264
+ g[x][y] = 1 ;
265
+ }
266
+ }
267
+ }
268
+ let ans = 0 ;
269
+ for (const row of g) {
270
+ for (const v of row) {
271
+ ans += v === 0 ? 1 : 0 ;
272
+ }
273
+ }
274
+ return ans;
275
+ }
276
+ ```
277
+
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 ++ ;
305
+ }
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 );
315
+ }
316
+ }
317
+
318
+ return m * n - guards .length - walls .length - c ;
319
+ }
320
+ ```
321
+
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];
333
+
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++ ;
339
+ }
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);
349
+ }
350
+ }
351
+
352
+ return m * n - guards .length - walls .length - c;
353
+ }
354
+ ```
355
+
245
356
<!-- tabs: end -->
246
357
247
358
<!-- solution: end -->
0 commit comments