Chris and Magic Square

Chris and Magic Square

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?

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 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

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
3
4 0 2
3 5 7
8 1 6
Output
9
Input
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
Output
1
Input
4
1 1 1 1
1 1 0 1
1 1 2 1
1 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.


首先我想要表达一下这道题我在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;
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通;、本 3项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看ReadmE.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、资 1源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READMe.m文件(如d有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值