-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi082.py
44 lines (40 loc) · 1.33 KB
/
i082.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
def ConquestCampaign(N, M, L, battalion):
def getNeighbors(cell):
neighbors = []
x = int(cell.split('_')[0])
y = int(cell.split('_')[1])
if x - 1 > 0:
fc = x - 1
neighbors.append(f'{fc}_{y}')
if x + 1 <= N:
fc = x + 1
neighbors.append(f'{fc}_{y}')
if y - 1 > 0:
sc = y - 1
neighbors.append(f'{x}_{sc}')
if y + 1 <= M:
sc = y + 1
neighbors.append(f'{x}_{sc}')
return neighbors
victoryDay = 1
if L == M * N:
return victoryDay
index = 1
conqueredCells = {}
while index <= L * 2:
key = str(battalion[index - 1]) + '_' + str(battalion[index])
conqueredCells[key] = False
index += 2
newConqueredCells = conqueredCells.copy()
while len(newConqueredCells) < M * N:
conqueredCells = newConqueredCells.copy()
for cell in conqueredCells:
if conqueredCells[cell] == False:
neighbors = getNeighbors(cell)
if neighbors != {}:
for neighbor in neighbors:
if not neighbor in conqueredCells:
newConqueredCells[neighbor] = False
newConqueredCells[cell] = True
victoryDay += 1
return victoryDay