A square matrix of size that contains only zeros and ones, and only zeros in the diagonal, is called a binary matrix.
The Laplacian of a binary matrix is another square matrix with the following content:
All cells (i.e. the diagonal of ), are equal to the number of ones in row of .
Any other cell in contains the same value than the corresponding cell in but with opposite sign (since contains only 0 and 1, these cells will contain 0 or -1 accordingly).
For example, the following binary matrix :
0 1 1 0 0
1 0 0 1 1
0 1 0 0 1
1 1 1 0 1
0 0 0 0 0
has as Laplacian the following Matrix:
2 -1 -1 0 0
-1 3 0 -1 -1
0 -1 2 0 -1
-1 -1 -1 4 -1
0 0 0 0 0
Write a function count_row(a,i) that receives a
binary matrix
and a row number
,
and returns how many 1 there are in the
-th
row of
.
Write a program that reads a sequence of binary matrices and
prints its Laplacian following the format shown in the examples. The
program must use the function count_row.
Input is a sequence of cases. A case is a number , the dimension of the coming binary matrix, followed by integers describing the matrix: all of them either 0 or 1, where all the diagonal entries are zero.
The output must contain the Laplacian transform of each of the matrices in the input in the same order. One empty line should appear after each case.
Input
2 0 1 1 0 3 0 0 0 0 0 0 0 0 0 3 0 1 0 0 0 1 1 1 0 3 0 1 1 0 0 1 1 1 0 4 0 1 1 0 1 0 0 1 1 1 0 1 0 1 1 0
Output
1 -1 -1 1 0 0 0 0 0 0 0 0 0 1 -1 0 0 1 -1 -1 -1 2 2 -1 -1 0 1 -1 -1 -1 2 2 -1 -1 0 -1 2 0 -1 -1 -1 3 -1 0 -1 -1 2
Input
3 0 0 1 0 0 1 0 0 0 4 0 0 0 0 1 0 0 0 1 1 0 0 1 1 1 0
Output
1 0 -1 0 1 -1 0 0 0 0 0 0 0 -1 1 0 0 -1 -1 2 0 -1 -1 -1 3