Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
sollution with numpy solution in 3rd party category for Network Attack by soyun
import numpy as np
import copy
def capture(matrix):
matrix = np.array(matrix)
state = copy.deepcopy(np.diag(matrix))
np.fill_diagonal(matrix,0)
t=0
while(np.sum(state) != 0):
state -= ((np.sum(matrix[state == 0],axis=0) > 0) & (state != 0)).astype(int)
t+=1
return t
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"
Feb. 20, 2018