Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题
B. Chris and Magic Square
题目连接:
http://www.codeforces.com/contest/711/problem/B
Description
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.
Sample Input
3
4 0 2
3 5 7
8 1 6
Sample Output
9
Hint
题意
给你一个\(n*n\)的矩阵,这个矩形是魔法矩阵的话,要求每一行,每一列,对角线的数的和都相同。
现在给你一个矩阵,里面恰好有一个空没有填数,问你是否能够使得这个矩阵成为魔法矩阵。
如果可以,输出应该填的数的大小。
题解:
先算出一行没有空的值的和,然后再去填这个数,然后再 去check每一行,每一列就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 510;
long long a[maxn][maxn];
int main()
{
int n,x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%lld",&a[i][j]);
if(a[i][j]==0)x=i,y=j;
}
if(n==1)
{
printf("1");
return 0;
}
int ax=1;
while(ax==x)ax++;
long long sum = 0;
for(int i=1;i<=n;i++)
sum+=a[ax][i];
long long tmp=0;
for(int i=1;i<=n;i++)
tmp+=a[x][i];
a[x][y]=sum-tmp;
if(a[x][y]<=0)
{
printf("-1\n");
return 0;
}
for(int i=1;i<=n;i++)
{
tmp=0;
for(int j=1;j<=n;j++)
tmp+=a[i][j];
if(tmp!=sum)
{
printf("-1\n");
return 0;
}
}
for(int j=1;j<=n;j++)
{
tmp=0;
for(int i=1;i<=n;i++)
tmp+=a[i][j];
if(tmp!=sum)
{
printf("-1\n");
return 0;
}
}
tmp = 0;
for(int i=1;i<=n;i++)
tmp+=a[i][i];
if(tmp!=sum)
{
printf("-1\n");
return 0;
}
tmp=0;
for(int i=1;i<=n;i++)
tmp+=a[i][n-i+1];
if(tmp!=sum)
{
printf("-1\n");
return 0;
}
printf("%lld\n",a[x][y]);
}
Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题的更多相关文章
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square (暴力)
Chris and Magic Square 题目链接: http://codeforces.com/contest/711/problem/B Description ZS the Coder an ...
- Codeforces Round #369 (Div. 2) A. Bus to Udayland (水题)
Bus to Udayland 题目链接: http://codeforces.com/contest/711/problem/A Description ZS the Coder and Chris ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题
C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...
- Codeforces Round #185 (Div. 2) A. Whose sentence is it? 水题
A. Whose sentence is it? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #313 (Div. 2) A. Currency System in Geraldion 水题
A. Currency System in Geraldion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...
- Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题
A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 水题
A. Meeting of Old Friends 题目连接: http://codeforces.com/contest/714/problem/A Description Today an out ...
随机推荐
- bzoj千题计划176:bzoj1199: [HNOI2005]汤姆的游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=1199 求出圆x的范围 把要判断的点按x从小到大排序 枚举图形 二分出x满足这个图形的一段区间 枚举这 ...
- JMS学习(三)ActiveMQ Message Persistence
1,JMS规范支持两种类型的消息传递:persistent and non-persistent.ActiveMQ在支持这两种类型的传递方式时,还支持消息的恢复.中间状态的消息(message are ...
- Chrome插件之ModHeader
一.ModHeader是什么 ModHeader顾名思义就是让我们可以自定义HTTP请求头或者是重写响应头,包括新增请求头/响应头或者覆盖Chrome浏览器设置的请求头的默认值,同时还可以根据URL ...
- networkManger介绍
http://www.linuxidc.com/Linux/2013-08/88809.htm
- MySQL删除数据后磁盘空间的释放情况【转】
OPTIMIZE TABLE 当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删除操作后在数据文件中留下碎片所致.OPTIMIZE TABLE 是指对表进行优化.如果已经删除 ...
- Python学习四|变量、对象、引用的介绍
变量 变量创建:一个变量也就是变量名,就像a,当代码第一次赋值时就创建了它.之后的赋值将会改变已创建的变量名的值,从技术上讲,Python在代码运行之前先检测变量名,可以当成是最初的赋值创建了变量. ...
- poj1033
模拟题,注意不需要移动的情况要特殊输出 #include <cstdio> #include <cstring> #include <cstdlib> using ...
- [转]mysql性能优化-慢查询分析、优化索引和配置
一. 优化概述 MySQL数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候.磁盘I/O瓶颈发生在装入数据远大于内存容量的时候,如果应用分布在 ...
- intellij 出现“Usage of API documented as @since 1.8+”的解决办法
intellij 出现“Usage of API documented as @since 1.8+”的解决办法 Usage of API documented as @since 1.8+ This ...
- 前后端分离之mockjs基本介绍
安装与使用 # 安装 npm install mockjs #使用 Mock var Mock = require('mockjs') var data = Mock.mock({ // 属性 lis ...