-
Notifications
You must be signed in to change notification settings - Fork 44
/
bomb-enemy.py
211 lines (195 loc) · 6.44 KB
/
bomb-enemy.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Given a 2D grid, each cell is either a wall 'Y', an enemy 'X' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.
# 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.
# Note that you can only put the bomb at an empty cell.
#
# Example:
#
# For the given grid
#
# 0 X 0 0
# X 0 Y X
# 0 X 0 0
#
# return 3. (Placing a bomb at (1,1) kills 3 enemies)
# V0
### NOTE : IT'S LIKE "BOMBERMAN", SO THE BOMB CAN ONLY EXPLODE "STRAIGHT" LINE,
# -> BFS IN THIS PROBLEM MAY NOT WORKS
class Solution(object):
def maxKilledEnemies(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
def count(x, y):
left, right, up, down = 0, 0, 0, 0
# left (<-)
for i in range(x-1, -1, -1):
if grid[i][y] == 'W':
break
if grid[i][y] == 'E':
left += 1
# right (->)
for i in range(x+1, row):
if grid[i][y] == 'W':
break
if grid[i][y] == 'E':
right += 1
# up (↑)
for j in range(y+1, col):
if grid[x][j] == 'W':
break
if grid[x][j] == 'E':
up += 1
# down (↓)
for j in range(y-1, -1, -1):
if grid[x][j] == 'W':
break
if grid[x][j] == 'E':
down += 1
return left+right+up+down
# base case
if not grid: return 0
row, col = len(grid), len(grid[0])
ans = 0
for i in range(row):
for j in range(col):
if grid[i][j] == '0':
# count the num of enemies
ans = max(ans, count(i, j))
return ans
# V0'
# TODO : OPTIMIZATION : NOT TO SEARCH "visited" grid REPEATEDLY
# V1
# https://www.twblogs.net/a/5c9baf03bd9eee73ef4b0e0d
class Solution(object):
def maxKilledEnemies(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
def count(x, y):
left, right, up, down = 0, 0, 0, 0
for i in range(x-1, -1, -1):
if grid[i][y] == 'W':
break
if grid[i][y] == 'E':
left += 1
for i in range(x+1, row):
if grid[i][y] == 'W':
break
if grid[i][y] == 'E':
right += 1
for j in range(y+1, col):
if grid[x][j] == 'W':
break
if grid[x][j] == 'E':
up += 1
for j in range(y-1, -1, -1):
if grid[x][j] == 'W':
break
if grid[x][j] == 'E':
down += 1
return left+right+up+down
# base case
if not grid: return 0
row, col = len(grid), len(grid[0])
ans = 0
for i in range(row):
for j in range(col):
if grid[i][j] == '0':
# count the num of enemies
ans = max(ans, count(i, j))
return ans
### Test case : dev
# V1'
# https://www.twblogs.net/a/5c9baf03bd9eee73ef4b0e0d
class Solution(object):
def maxKilledEnemies(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
# base case
if not grid: return 0
row, col = len(grid), len(grid[0])
ans = 0
arr = [[0]*col for i in range(row)]
# from left to right
for i in range(row):
count = 0
for j in range(col):
if grid[i][j] == 'E':
count += 1
elif grid[i][j] == 'W':
count = 0
else:
arr[i][j] += count
# from right to left
for i in range(row):
count = 0
for j in range(col-1, -1, -1):
if grid[i][j] == 'E':
count += 1
elif grid[i][j] == 'W':
count = 0
else:
arr[i][j] += count
# from up to down
for j in range(col):
count = 0
for i in range(row):
if grid[i][j] == 'E':
count += 1
elif grid[i][j] == 'W':
count = 0
else:
arr[i][j] += count
# from down to up
for j in range(col):
count = 0
for i in range(row-1, -1, -1):
if grid[i][j] == 'E':
count += 1
elif grid[i][j] == 'W':
count = 0
else:
arr[i][j] += count
ans = max(ans, arr[i][j])
return ans
# V2
# Time: O(m * n)
# Space: O(m * n)
class Solution(object):
def maxKilledEnemies(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
result = 0
if not grid or not grid[0]:
return result
down = [[0 for _ in range(len(grid[0]))] for _ in range(len(grid))]
right = [[0 for _ in range(len(grid[0]))] for _ in range(len(grid))]
for i in reversed(range(len(grid))):
for j in reversed(range(len(grid[0]))):
if grid[i][j] != 'W':
if i + 1 < len(grid):
down[i][j] = down[i + 1][j]
if j + 1 < len(grid[0]):
right[i][j] = right[i][j + 1]
if grid[i][j] == 'E':
down[i][j] += 1
right[i][j] += 1
up = [0 for _ in range(len(grid[0]))]
for i in range(len(grid)):
left = 0
for j in range(len(grid[0])):
if grid[i][j] == 'W':
up[j], left = 0, 0
elif grid[i][j] == 'E':
up[j] += 1
left += 1
else:
result = max(result,
left + up[j] + right[i][j] + down[i][j])
return result