This repository was archived by the owner on May 16, 2021. It is now read-only.
forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbomb-enemy_1_AC.cpp
78 lines (72 loc) · 2.08 KB
/
bomb-enemy_1_AC.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// If you know exactly what you're doing, do it right.
// If you don't, think it through.
#include <algorithm>
#include <vector>
using std::max;
using std::vector;
class Solution {
public:
int maxKilledEnemies(vector<vector<char>>& grid) {
auto &a = grid;
int n = a.size();
int m = (n > 0 ? a[0].size() : 0);
if (n == 0 || m == 0) {
return 0;
}
vector<vector<int>> h(n, vector<int>(m));
vector<vector<int>> v(n, vector<int>(m));
int i, j;
int k;
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
if (a[i][j] == 'W') {
h[i][j] = 0;
continue;
}
if (j > 0 && (a[i][j - 1] == '0' || a[i][j - 1] == 'E')) {
h[i][j] = h[i][j - 1];
continue;
}
k = j;
h[i][j] = 0;
while (k < m && a[i][k] != 'W') {
if (a[i][k] == 'E') {
++h[i][j];
}
++k;
}
}
}
for (j = 0; j < m; ++j) {
for (i = 0; i < n; ++i) {
if (a[i][j] == 'W') {
v[i][j] = 0;
continue;
}
if (i > 0 && (a[i - 1][j] == '0' || a[i - 1][j] == 'E')) {
v[i][j] = v[i - 1][j];
continue;
}
k = i;
v[i][j] = 0;
while (k < n && a[k][j] != 'W') {
if (a[k][j] == 'E') {
++v[i][j];
}
++k;
}
}
}
int res = 0;
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
if (a[i][j] == '0') {
res = max(res, h[i][j] + v[i][j]);
}
}
}
h.clear();
v.clear();
return res;
}
};