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?
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 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
非常考验对细节的把握
尤其是当n为1,输入0时这一特殊情况!
我的思路就是
因为只可能有一个值,那么我先算出这个值!
代入,看符合要求不,这样的话应该会简单点
下面代码(细节问题写在注释里)
#include<stdio.h>
#include<string.h>
long long map[510][510];
long long vish[510];
long long viss[510];
long long visx[5];
int main()
{
int n,x,y;
long long num,num0; //注意开的数组是longlong型的,int存不下!
while(~scanf("%d",&n))
{
num=0,num0=0;
memset(vish,0,sizeof(vish));//记得清零操作!
memset(visx,0,sizeof(visx));
memset(viss,0,sizeof(viss));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
scanf("%lld",&map[i][j]);
if(map[i][j]==0)
{
x=i;
y=j;
}
}
}
if(n==1)
{
printf("1\n");
continue;
}
for(int i=1;i<=n;i++)
{
num0+=map[x][i];
}
for(int i=1; i<=n; i++) //先选出一个基准数
{
if(i!=x)
{
for(int j=1;j<=n;j++)
{
num+=map[i][j];
}
break;
}
}
map[x][y]=num-num0;//将空的数填上
if(map[x][y]<=0) //注意,要判断填入的数是否是正整数,不是直接就-1
{
printf("-1\n");
continue;
}
//printf("%lld %lld\n",map[x][y],num);
visx[1]=0,visx[2]=0;
int flag=0;
for(int i=1,j=n; i<=n,j>=1; i++,j--) //求对角线值
{
visx[1]+=map[i][i];
visx[2]+=map[i][j];
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
viss[i]+=map[j][i];//求每竖列值
vish[i]+=map[i][j];//求每横列值
}
}
flag=0;
for(int i=1;i<=n;i++)
{
if(viss[i]!=num||vish[i]!=num) //判断每一行,每一列是否相同
{
//printf("%lld %lld\n",viss[i],vish[i]);
flag=1;
printf("-1\n");
break;
}
}
if(visx[1]!=num&&flag==0||visx[2]!=num&&flag==0) //判断对角线是否相同!
{
flag=1;
printf("-1\n");
continue;
}
if(flag==0)
{
printf("%lld\n",map[x][y]);
}
}
}