Skip to content

Commit 5a6aba1

Browse files
committed
Time: 26 ms (56.14%), Space: 80.8 MB (6.02%) - LeetHub
1 parent 36229ff commit 5a6aba1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
3+
4+
int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
5+
char[][] matrix = new char[m][n];
6+
int ans = m*n-guards.length-walls.length;
7+
8+
for(int[] wall : walls){
9+
matrix[wall[0]][wall[1]]='W';
10+
}
11+
for(int[] guard : guards){
12+
matrix[guard[0]][guard[1]]='G';
13+
}
14+
15+
for(int[] point : guards){
16+
for(int[] dir : dirs){
17+
int nrow =point[0]+dir[0];
18+
int ncol =point[1]+dir[1];
19+
20+
while(!(nrow<0 || ncol<0 || nrow>=m || ncol>=n ||
21+
matrix[nrow][ncol]=='G' || matrix[nrow][ncol]=='W')){
22+
23+
if(matrix[nrow][ncol]!='P'){
24+
ans--;
25+
}
26+
matrix[nrow][ncol]='P';
27+
nrow +=dir[0];
28+
ncol +=dir[1];
29+
}
30+
}
31+
}
32+
return ans;
33+
}
34+
}

0 commit comments

Comments
 (0)