forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bomb_Enemy.cpp
54 lines (50 loc) · 1.55 KB
/
Bomb_Enemy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
int maxKilledEnemies(vector<vector<char>>& grid) {
int m = grid.size();
if(m == 0) return 0;
int n = grid[0].size();
if(n == 0) return 0;
vector<vector<int>> row(m, vector<int>(n, 0));
vector<vector<int>> col(m, vector<int>(n, 0));
for(int i = 0; i < m; ++i) {
int cnt = 0;
for(int j = 0; j < n; ++j) {
int start = j;
while(j < n and grid[i][j] != 'Y') {
cnt += (grid[i][j] == 'X'); ++j;
}
for(int k = start; k < j; ++k) {
if(grid[i][k] == '0') {
row[i][k] = cnt;
}
}
cnt = 0;
}
}
for(int j = 0; j < n; ++j) {
int cnt = 0;
for(int i = 0; i < m; ++i) {
int start = i;
while(i < m and grid[i][j] != 'Y') {
cnt += (grid[i][j] == 'X'); ++i;
}
for(int k = start; k < i; ++k) {
if(grid[k][j] == '0') {
col[k][j] = cnt;
}
}
cnt = 0;
}
}
int result = 0;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(grid[i][j] == '0') {
result = max(result, row[i][j] + col[i][j]);
}
}
}
return result;
}
};