ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × 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 a positive 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 form a 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?
InputThe 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 the i-th of them denotes ai, j(1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.
It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.
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.
3 4 0 2 3 5 7 8 1 6
9
4 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
1
4 1 1 1 1 1 1 0 1 1 1 2 1 1 1 1 1
-1
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.
首先我想要表达一下这道题我在wa了29次之后终于在第30次A了!!!!!!!!!!!!!!
先抹一发心酸泪,然后下面开始进入正题。
题目大意:给了一个n*n的数组,数组中有一个位置的数为0,现在要你在0这里填入一个大于0小于等于10^18的数,使得这个数组每行,每列,和对角线上数字的和相等;
其实题目很简单,就是里面有一些要注意的细节。
然后下面是我wa出来的总结:
1.要对n==1时进行特判,n==1时直接输出1就行,没有特判的话会在第4组数据wa。(我也就wa了个14次就发现了,也没有特别多的样子对不对)
2.题目要求输出1到10^18的数或者-1,所以如果得到的结果是0或者负数或者没有结果都要输出-1.
如果没注意这里的话会在第7组数据wa。(这里我也就wa了个12次嘛)
3.这里主要是我自己没注意,读题的时候其实读到了,但是做的时候忘记了,然后就判的是横排的相等,竖排的相等,斜着的两排相等,而不是都相等,这里错了的话会在88组wa。
4.我之前改的时候是对结果等于0时进行了特判,没有注意结果小于0的情况,就在94组wa了两次,最后终于过了。
最后进行一下反思:看题,做题的时候一定要细心一点。这道题是在比赛的时候做的,主要是心态有点崩,改了很多次一直没有改到应该改的地方。然后最后比赛结束的时候还没做出来,是结束后再回来看才A的,下次再遇到这种情况会好好注意的,不盲目的去改。
下面放代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long map[505][505];
int main()
{
int n;
scanf("%d",&n);
int h,l,flag=0,p=0;
long long ans=0,k=0,k1=0,k2=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
scanf("%I64d",&map[i][j]);
if(map[i][j]==0)
h=i,l=j;
}
if(n==1)
printf("1\n");
else
{
for(int i=0; i<n; i++)
{
ans=0;
if(i!=h)
{
for(int j=0; j<n; j++)
ans+=map[i][j];
if(p==0)
k=ans;
p=1;
if(ans!=k)
{
flag=1;
break;
}
}
}
if(flag)
printf("-1\n");
else
{
ans=0;
for(int i=0; i<n; i++)
ans+=map[h][i];
map[h][l]=k-ans;
if(map[h][l]<=0)
printf("-1\n");
else
{
for(int i=0; i<n; i++)
{
ans=0;
for(int j=0; j<n; j++)
ans+=map[j][i];
if(ans!=k)
{
flag=1;
break;
}
}
if(flag)
printf("-1\n");
else
{
for(int i=0; i<n; i++)
k1+=map[i][i];
for(int i=0; i<n; i++)
k2+=map[i][n-1-i];
if(k1==k&&k2==k)
printf("%I64d\n",map[h][l]);
else
printf("-1\n");
}
}
}
}
return 0;
}