forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBombEnemy.py
More file actions
72 lines (66 loc) · 2.47 KB
/
BombEnemy.py
File metadata and controls
72 lines (66 loc) · 2.47 KB
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
#Bomb Enemy
#Difficulty:Medium
#
#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.
#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 E 0 0
#E 0 W E
#0 E 0 0
#
#return 3. (Placing a bomb at (1,1) kills 3 enemies)
class Approch1(object):
def __init__(self, matrix):
self.matrix = matrix
self.output = [[0 for col in range(len(matrix[0]))]for row in range(len(matrix))]
self.killMax = 0
self.rowMax = None
self.colMax = None
def updateOutput(self, matrixCell, enemy, row, col):
if matrixCell == "E":
return enemy+1
if matrixCell == "W":
return 0
self.output[row][col] = enemy+self.output[row][col]
#save max
if (self.killMax < self.output[row][col]):
self.killMax = self.output[row][col]
self.rowMax = row
self.colMax = col
return enemy
def logic(self):
#scan row from left to right
for row in range(len(self.matrix)):
enemy = 0
for col in range(len(self.matrix[0])):
enemy = self.updateOutput(self.matrix[row][col], enemy, row, col)
#scan row from right to left
for row in range(len(self.matrix)):
enemy = 0
for col in range(len(self.matrix), -1, -1):
enemy = self.updateOutput(self.matrix[row][col], enemy, row, col)
#Scan column from up to down
for col in range(len(self.matrix[0])):
enemy = 0
for row in range(len(self.matrix)):
enemy = self.updateOutput(self.matrix[row][col], enemy, row, col)
#Scan column from down to up
for col in range(len(self.matrix[0])):
enemy = 0
for row in range(len(self.matrix)-1, -1, -1):
enemy = self.updateOutput(self.matrix[row][col], enemy, row, col)
print "The row and col where max enemies will be killed is ",
print self.rowMax, self.colMax
print "the max enemies killed is ",
print self.killMax
#Main Program
m = [[0, "E", 0, 0],
["E", 0, "W", "E"],
[0, "E", 0, 0]]
m = [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
o = Approch1(m)
o.logic()