Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Network Attack by yuyin110
def capture(matrix):
N = len(matrix)
infection = [0]
time = 1
while len(infection) < N:
reduce = []
new_infection = []
for num in infection:
for i in range(N):
if matrix[num][i] == 1 and num != i: ########if connect with the num computer
if i not in reduce:################## if the num computer was not infected, the security levels reduce 1
reduce.append(i)
matrix[i][i] -= 1
if matrix[i][i] == 0: ################## if connect num have not security levels, add to infection computer
if i not in new_infection:
new_infection.append(i)
infection.extend(new_infection)
if len(infection) == N:
return time
else:
time += 1
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert capture([[0, 1, 0, 1, 0, 1],
[1, 8, 1, 0, 0, 0],
[0, 1, 2, 0, 0, 1],
[1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 3, 1],
[1, 0, 1, 0, 1, 2]]) == 8, "Base example"
assert capture([[0, 1, 0, 1, 0, 1],
[1, 1, 1, 0, 0, 0],
[0, 1, 2, 0, 0, 1],
[1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 3, 1],
[1, 0, 1, 0, 1, 2]]) == 4, "Low security"
assert capture([[0, 1, 1],
[1, 9, 1],
[1, 1, 9]]) == 9, "Small"
June 15, 2018