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
| n = int(input())
matrix = [] ##define a matrix
for i in range(n):
line = input()
line_ = []
for sign in line:
line_.append(sign)
matrix.append(line_)
m = int(input())
#define a function, which means the pace of infection
def virus(x,y,virus_people):
for (i,j) in [(x-1,y), (x+1,y), (x, y-1), (x, y+1)]: ##rooms around
if i < 0 or i >= n or j < 0 or j >= n: ##set boundary conditions
continue
element = matrix[i][j]
if element == '.': ##added infected room into the list
virus_people.append((i,j))
virus_sum = 0 #set sum of the infected people
for i in range(n): ##record the initial number of infections
for j in range(n):
if matrix[i][j] == '@':
virus_sum += 1
for day in range(m-1): ##start the infect function
virus_people = []
for i in range(n):
for j in range(n):
if matrix[i][j] == '@':
virus(i,j, virus_people)
virus_people = list(set(virus_people)) ##remove individuals re-infected each time
for x, y in virus_people:
matrix[x][y] = '@'
virus_sum += len(virus_people)
print(virus_sum)
|