forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_361.java
74 lines (62 loc) · 2.39 KB
/
_361.java
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
package com.fishercoder.solutions;
public class _361 {
public static class Solution1 {
public int maxKilledEnemies(char[][] grid) {
int m = grid.length;
if (grid == null || m == 0) {
return 0;
}
int n = grid[0].length;
int[][] max = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '0') {
int count = 0;
//count all possible hits in its upward direction
for (int k = j - 1; k >= 0; k--) {
if (grid[i][k] == 'E') {
count++;
} else if (grid[i][k] == 'W') {
break;
}
}
//count all possible hits in its downward direction
for (int k = j + 1; k < n; k++) {
if (grid[i][k] == 'E') {
count++;
} else if (grid[i][k] == 'W') {
break;
}
}
//count all possible hits in its right direction
for (int k = i + 1; k < m; k++) {
if (grid[k][j] == 'E') {
count++;
} else if (grid[k][j] == 'W') {
break;
}
}
//count all possible hits in its left direction
for (int k = i - 1; k >= 0; k--) {
if (grid[k][j] == 'E') {
count++;
} else if (grid[k][j] == 'W') {
break;
}
}
max[i][j] = count;
}
}
}
int result = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (max[i][j] > result) {
result = max[i][j];
}
}
}
return result;
}
}
}