|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 361. Bomb Enemy |
| 5 | + * |
4 | 6 | * Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.
|
5 | 7 | The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.
|
6 | 8 | Note that you can only put the bomb at an empty cell.
|
|
16 | 18 | */
|
17 | 19 | public class _361 {
|
18 | 20 |
|
19 |
| - public int maxKilledEnemies(char[][] grid) { |
20 |
| - int m = grid.length; |
21 |
| - if (grid == null || m == 0) { |
22 |
| - return 0; |
23 |
| - } |
24 |
| - int n = grid[0].length; |
| 21 | + public static class Solution1 { |
| 22 | + public int maxKilledEnemies(char[][] grid) { |
| 23 | + int m = grid.length; |
| 24 | + if (grid == null || m == 0) { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + int n = grid[0].length; |
25 | 28 |
|
26 |
| - int[][] max = new int[m][n]; |
| 29 | + int[][] max = new int[m][n]; |
27 | 30 |
|
28 |
| - for (int i = 0; i < m; i++) { |
29 |
| - for (int j = 0; j < n; j++) { |
| 31 | + for (int i = 0; i < m; i++) { |
| 32 | + for (int j = 0; j < n; j++) { |
30 | 33 |
|
31 |
| - if (grid[i][j] == '0') { |
32 |
| - int count = 0; |
| 34 | + if (grid[i][j] == '0') { |
| 35 | + int count = 0; |
33 | 36 |
|
34 |
| - //count all possible hits in its upward direction |
35 |
| - for (int k = j - 1; k >= 0; k--) { |
36 |
| - if (grid[i][k] == 'E') { |
37 |
| - count++; |
38 |
| - } else if (grid[i][k] == 'W') { |
39 |
| - break; |
| 37 | + //count all possible hits in its upward direction |
| 38 | + for (int k = j - 1; k >= 0; k--) { |
| 39 | + if (grid[i][k] == 'E') { |
| 40 | + count++; |
| 41 | + } else if (grid[i][k] == 'W') { |
| 42 | + break; |
| 43 | + } |
40 | 44 | }
|
41 |
| - } |
42 | 45 |
|
43 |
| - //count all possible hits in its downward direction |
44 |
| - for (int k = j + 1; k < n; k++) { |
45 |
| - if (grid[i][k] == 'E') { |
46 |
| - count++; |
47 |
| - } else if (grid[i][k] == 'W') { |
48 |
| - break; |
| 46 | + //count all possible hits in its downward direction |
| 47 | + for (int k = j + 1; k < n; k++) { |
| 48 | + if (grid[i][k] == 'E') { |
| 49 | + count++; |
| 50 | + } else if (grid[i][k] == 'W') { |
| 51 | + break; |
| 52 | + } |
49 | 53 | }
|
50 |
| - } |
51 | 54 |
|
52 |
| - //count all possible hits in its right direction |
53 |
| - for (int k = i + 1; k < m; k++) { |
54 |
| - if (grid[k][j] == 'E') { |
55 |
| - count++; |
56 |
| - } else if (grid[k][j] == 'W') { |
57 |
| - break; |
| 55 | + //count all possible hits in its right direction |
| 56 | + for (int k = i + 1; k < m; k++) { |
| 57 | + if (grid[k][j] == 'E') { |
| 58 | + count++; |
| 59 | + } else if (grid[k][j] == 'W') { |
| 60 | + break; |
| 61 | + } |
58 | 62 | }
|
59 |
| - } |
60 | 63 |
|
61 |
| - //count all possible hits in its left direction |
62 |
| - for (int k = i - 1; k >= 0; k--) { |
63 |
| - if (grid[k][j] == 'E') { |
64 |
| - count++; |
65 |
| - } else if (grid[k][j] == 'W') { |
66 |
| - break; |
| 64 | + //count all possible hits in its left direction |
| 65 | + for (int k = i - 1; k >= 0; k--) { |
| 66 | + if (grid[k][j] == 'E') { |
| 67 | + count++; |
| 68 | + } else if (grid[k][j] == 'W') { |
| 69 | + break; |
| 70 | + } |
67 | 71 | }
|
68 |
| - } |
69 |
| - |
70 |
| - max[i][j] = count; |
71 | 72 |
|
| 73 | + max[i][j] = count; |
| 74 | + } |
72 | 75 | }
|
73 | 76 | }
|
74 |
| - } |
75 | 77 |
|
76 |
| - int result = 0; |
| 78 | + int result = 0; |
77 | 79 |
|
78 |
| - for (int i = 0; i < m; i++) { |
79 |
| - for (int j = 0; j < n; j++) { |
80 |
| - if (max[i][j] > result) { |
81 |
| - result = max[i][j]; |
| 80 | + for (int i = 0; i < m; i++) { |
| 81 | + for (int j = 0; j < n; j++) { |
| 82 | + if (max[i][j] > result) { |
| 83 | + result = max[i][j]; |
| 84 | + } |
82 | 85 | }
|
83 | 86 | }
|
| 87 | + return result; |
84 | 88 | }
|
85 |
| - return result; |
86 | 89 | }
|
87 | 90 | }
|
0 commit comments