-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBombEnemy.py
61 lines (59 loc) · 1.65 KB
/
BombEnemy.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
class Solution:
def maxKilledEnemies(self, grid: List[List[str]]) -> int:
'''
brute force - ac
'''
def explode(xx,yy):
dead = set()
r = 0
#u
x = xx
y = yy
while 0<=x<m:
if grid[x][y] == 'E' and (x,y) not in dead:
r += 1
dead.add((x,y))
if grid[x][y] == 'W':
break
x -= 1
#d
x = xx
y = yy
while 0<=x<m:
if grid[x][y] == 'E' and (x,y) not in dead:
r += 1
dead.add((x,y))
if grid[x][y] == 'W':
break
x += 1
#l
x = xx
y = yy
while 0<=y<n:
if grid[x][y] == 'E' and (x,y) not in dead:
r += 1
dead.add((x,y))
if grid[x][y] == 'W':
break
y -= 1
#r
x = xx
y = yy
while 0<=y<n:
if grid[x][y] == 'E' and (x,y) not in dead:
r += 1
dead.add((x,y))
if grid[x][y] == 'W':
break
y += 1
return r
if len(grid) == 0 or len(grid[0]) == 0:
return 0
m = len(grid)
n = len(grid[0])
res = 0
for i in range(m):
for j in range(n):
if grid[i][j] == '0':
res = max(res,explode(i,j))
return res