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]);elseprintf("-1\n");}}}}return 0;
}

Chris and Magic Square相关推荐

  1. cf369 B Chris and Magic Square

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

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

  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. CodeForces - 711B Chris and Magic Square

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

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

  6. 文章标题 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 ...

  7. 【codeforces 711B】Chris and Magic Square

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

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

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

  9. Codeforces 711B- Chris and Magic Square

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

最新文章

  1. php webserver documentroot,PHP $_SERVER['DOCUMENT_ROOT'] 问题
  2. Safari浏览器的智能跟踪预防工作原理
  3. 如何解决Contacts中的多音字排序错误问题
  4. Java中的作用域有哪些
  5. 博客会被搬去csdn
  6. 当交易所开始“革命”,整个行业将“为之一颤”
  7. html游戏禁止微信浏览器下拉,如何用电脑模拟微信浏览器浏览禁止PC打开的微网站...
  8. Mac解决终端显示乱码
  9. java编译找不到符号_关于久违的Javac,编译出现“找不到符号”
  10. 【数据库实验】《小型MIS的开发》— JavaFx 开发 民航票务管理系统
  11. 20130912计划
  12. java web初级面试题_Java Web应用程序初学者教程
  13. vue2.0配置 https://github.com/wike933/vuebook
  14. mac上键盘说明以及intellij 快捷键的使用
  15. 求解无约束最优化问题的共轭梯度法matlab程序,Matlab实现FR共轭梯度法
  16. Windows 11 新功能 Microsoft Teams
  17. 在Markdown文件中快速插入本地图片
  18. Unity如果制作特效
  19. 图形界面介绍Floorplan ToolBox
  20. Axios GET 不能设置Content-Type

热门文章

  1. 【FLASH存储器系列十三】Nand flash出厂就有坏块,NOR flash有吗?
  2. 七、脉冲编码调制:采样、量化、编码 ——网络工程师成长之路
  3. 想做一个手机点歌的程序,希望大家进来指点
  4. 泰拉瑞亚手机版html,泰拉瑞亚手机版幽灵套装怎么做 幽灵套装ID和属性详解
  5. CCNA培训(四)20210718day04
  6. 每日资源分享(彩虹外链PHP网盘V5.4更新 新增用户系统与分块上传)
  7. bind9+mysql搭建高可用DNS解析服务
  8. jsp交通事故档案管理系统计算机毕业设计(源码、运行环境)
  9. 同时导出多个excel,并且一个excel中包含多个sheet
  10. C/C++中的字符串比较函数strcmp/memcmp/CString.Compare/CString:CompareNoCase