B. Chris and Magic Square
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

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

思路:

题意是,给你一个N*N矩阵,其中0代表未知的那个数,让你在这个位置填上一个数使整个矩阵的 每一行、每一列、主对角线、次对角线的和都相等。只需要分情况讨论,n=1的时候这个数可以是任意正整数,n=2的时候就暴力求就好,n>=3的时候,输入时候先保存0这个点的横纵坐标,然后再选择三列求和,其中相等的两列的值就是所有和都要满足的那个值。然后找到0所在列,把列里的所有元素减去就得到了0这个点应该放进去的数。(不用担心会减去0这个点,因为0对计算结果无影响)。然后就是要检验了,先吧0所在点换为要放进去的数,然后对每一行每一列还有两个对角线都和之前算出来的那个和进行比较,全部相等的时候才说明能放进去。而且要注意这里放下去的数不能是非整数。我的bug出现在最后的一个flag标志上,判断条件if(flag==true)我少打了一个等号结果WA了...粗心害死人啊。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;long long a[500][500];int main()
{//freopen("in.txt","r",stdin);int n;scanf("%d",&n);if(n==1){for(int i=0;i<n;i++)for(int j=0;j<n;j++){scanf("ll%d",&a[i][j]);}printf("1\n");}if(n==2){long long flag1=0,flag2=0;for(int i=0;i<n;i++)for(int j=0;j<n;j++){scanf("%lld",&a[i][j]);if(a[i][j]==0){flag1=i;flag2=j;}}long long result=0;long long sum[2]={0,0};for(int i=0;i<2;i++)for(int j=0;j<2;j++){sum[i]+=a[i][j];}if(sum[0]>sum[1]){result=sum[0]-sum[1];a[flag1][flag2]=result;}else {result=sum[1]-sum[0];a[flag1][flag2]=result;}long long a1=a[0][0]+a[1][1];long long a2=a[0][0]+a[0][1];long long a3=a[1][0]+a[1][1];long long a4=a[1][0]+a[0][0];long long a5=a[1][0]+a[0][1];long long a6=a[0][1]+a[1][1];if(a1==a2&&a2==a3&&a3==a4&&a4==a5&&a5==a6&&a[flag1][flag2]>0)printf("%lld\n",result);else printf("-1\n");}else if(n>=3){long long flag1=0,flag2=0;for(int i=0;i<n;i++)for(int j=0;j<n;j++){scanf("%lld",&a[i][j]);if(a[i][j]==0){flag1=i;flag2=j;}}long long sum[3]={0,0,0};for(int i=0;i<3;i++){for(int j=0;j<n;j++){sum[i]+=a[i][j];}}long long result=0;if(sum[0]==sum[1])result=sum[0];else if(sum[0]==sum[2])result=sum[0];else if(sum[1]==sum[2])result=sum[1];for(int j=0;j<n;j++){result=result-a[flag1][j];}a[flag1][flag2]=result;long long jianyan=0;for(int j=0;j<n;j++)jianyan+=a[flag1][j];bool rrr=true;for(int i=0;i<n;i++){long long e=0;for(int j=0;j<n;j++){e+=a[i][j];}if(e!=jianyan){rrr=false;break;}}if(rrr==true){for(int j=0;j<n;j++){long long e=0;for(int i=0;i<n;i++){e+=a[i][j];}if(e!=jianyan){rrr=false;break;}}}if(rrr==true){long long e=0;for(int i=0;i<n;i++){e+=a[i][i];}if(e!=jianyan){rrr=false;}}if(rrr==true){long long e=0;for(int i=0;i<n;i++){e+=a[i][n-i-1];}if(e!=jianyan){rrr=false;}}if(rrr==true&&a[flag1][flag2]>0)printf("%lld\n",result); else printf("-1\n");  }
}

CodeForces 711B. Chris and Magic Square(水题)相关推荐

  1. CodeForces - 711B Chris and Magic Square

    CodeForces - 711B  Chris and Magic Square   题意:给你一个N*N矩阵,其中0代表未知的那个数,让你在这个位置填上一个数使整个矩阵的 每一行.每一列.主对角线 ...

  2. codeforces 711B - Chris and Magic Square(矩阵0位置填数)

    题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...

  3. Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. Chris and Magic Square

    Chris and Magic Square ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There ...

  5. cf369 B Chris and Magic Square

    B. Chris and Magic Square time limit per test2 seconds memory limit per test256 megabytes inputstand ...

  6. B. Chris and Magic Square

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. Chris and Magic Square CodeForces - 711B

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  8. Educational Codeforces Round 7 B. The Time 水题

    B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...

  9. 文章标题 Chris and Magic Square

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  10. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

最新文章

  1. 什么原数据更容易平稳_【时间序列】-航空数据预测
  2. 使用 JMeter 进行压力测试
  3. mysql100万数据一键下载csv_使用PHP来导入包含100万条数据的csv文件,请问你最快多久能全部导入mysql 数据库?...
  4. VSCode中Flow报错解决
  5. Connection closed by foreign host. 误改BUG
  6. 【必看】局域网ip地址不够用怎么办?
  7. wxWidgets:exec 示例演示 wxExecute 和相关函数
  8. Python编程专属骚技巧7
  9. 22行代码AC,三种解法——例题3-6_环状序列(UVa-1584)
  10. css Flex布局(一)
  11. loadrunner可用许可证
  12. synchronized 线程同步,添加对象锁与类锁
  13. sysbench压测cpu,io,memory,threads,mutex
  14. 十大中文搜索引擎排名,头一个你绝对意想不到!
  15. java 临时文件目录_在Java中使用临时文件/文件夹
  16. LED MEO GTO GEO IGSO SSO概念
  17. Apache多后缀解析漏洞复现(apache_parsing_vulnerability)
  18. CF31D Chocolate 解题报告 *
  19. hadoop-mapreduce-example中实例介绍
  20. 概念辨析:dBm, dBi, dBd, dB, dBc,dBuV,dBuVemf

热门文章

  1. 求s = a + aa + aaa +aaaa +……a的值
  2. 百看不如一练, 247 个 Python 实战案例(附源代码)
  3. 微信为什么不禁拼多多?诱导分享到底怎么判
  4. Static Asynchronous Component Misuse Detection for Android Applications
  5. 给一个字符串,将其按照单词顺序进行反转
  6. win7找不到win10计算机图标,win7电脑桌面图标不见了怎么办
  7. dmac学习之基于LLI的multi block tranfer验证
  8. ZOJ-3939 The Lucky Week
  9. 单克隆抗体WuT9/甘草次酸-氟尿嘧啶偶联顺铂/RGD肽修饰聚谷氨酸-顺铂复合物的制备
  10. centos8 配置 dns_上网慢?你可能要改一下DNS