Chris and Magic Square

来源:互联网 发布:网络知识产权专利申请 编辑:程序博客网 时间:2024/06/04 09:01

ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is an × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill apositive integer into the empty cell.

Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid forma magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal — and the secondary diagonal —) are equal.

Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

n lines follow, each of them contains n integers. The j-th number in thei-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in thei-th row and j-th column of the magic grid. If the corresponding cell is empty,ai, j will be equal to0. Otherwise, ai, j ispositive.

It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such thatai, j = 0.

Output

Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

If there are multiple solutions, you may print any of them.

Example
Input
34 0 23 5 78 1 6
Output
9
Input
41 1 1 11 1 0 11 1 1 11 1 1 1
Output
1
Input
41 1 1 11 1 0 11 1 2 11 1 1 1
Output
-1
Note

In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,

The sum of numbers in each row is:

4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.

The sum of numbers in each column is:

4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.

The sum of numbers in the two diagonals is:

4 + 5 + 6 = 2 + 5 + 8 = 15.

In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.

题意是让你在一个矩阵里面填一个数,使得每行每列还有两条对角线的和相等
思路:用一个数组sum3存,分别用每一行的和减去0所在的那一行的和,还有每一列的和减去0所在那一列的和,看看都一样不,一样的话就看看两条对角线和0有关系没,有关系就加上值,再比较两条对角线相等不,相等输出值,不想等就ˉ1
反思:怀疑自己代码有问题,愣是没想到为1的时候,导致浪费了那么长的时间调试,不应该

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;#define LL long longint a[1000][1000];LL sum[1000000];LL sum2[1000000];LL sum3[1000000];int main(){    int n;    while(scanf("%d",&n)!=-1)    {        int p,p1;        memset(sum,0,sizeof(sum));        memset(sum2,0,sizeof(sum2));        memset(sum3,0,sizeof(sum3));        for(int i=1; i<=n; i++)        {            LL sum1=0;            for(int j=1; j<=n; j++)            {                scanf("%d",&a[i][j]);                if(a[i][j]==0)                {                    p=i;                    p1=j;                }                sum1+=a[i][j];            }            sum[i]=sum1;        }        if(n==1)        {            printf("1\n");        }        else        {        for(int i=1; i<=n; i++)        {            LL sum1=0;            for(int j=1; j<=n; j++)            {                sum1+=a[j][i];            }            sum2[i]=sum1;        }        LL we=0;        int r=0;        for(int i=1; i<=n; i++)        {            if(i==p&&i==p1)            {                r=1;            }            we+=a[i][i];        }       LL ni=0;       int e=0;       for(int i=1;i<=n;i++)       {           if(i==p&&(n-i+1)==p1)            e=1;           ni+=a[i][n-i+1];       }        int t=0;        for(int i=1; i<=n; i++)        {            if(i!=p)            {                sum3[t++]=sum[i]-sum[p];            }        }        for(int i=1; i<=n; i++)        {            if(i!=p1)                sum3[t++]=sum2[i]-sum2[p1];        }        sort(sum3,sum3+t);        int za=0;        for(int i=1; i<t; i++)        {            if(sum3[i]!=sum3[i-1]||sum3[i]<=0)            {                za=1;                break;            }        }        if(za==1)        {            printf("-1\n");        }        else        {            LL haha=sum[p]+sum3[0];            if(e==1)            {                ni+=sum3[0];            }           if(r==1)            {                we+=sum3[0];            }          if(we==ni&&we==haha)            {                printf("%lld\n",sum3[0]);            }            else            {                printf("-1\n");            }        }        }    }

0 0
原创粉丝点击