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 ...
随机推荐
- MySQL异步复制延迟解决
http://www.ttlsa.com/mysql/mysql-5-7-enhanced-multi-thread-salve/
- [机器学习&数据挖掘]机器学习实战决策树plotTree函数完全解析
在看机器学习实战时候,到第三章的对决策树画图的时候,有一段递归函数怎么都看不懂,因为以后想选这个方向为自己的职业导向,抱着精看的态度,对这本树进行地毯式扫描,所以就没跳过,一直卡了一天多,才差不多搞懂 ...
- 【51Nod】1055 最长等差数列 动态规划
[题目]1055 最长等差数列 [题意]给定大小为n的互不不同正整数集合,求最长等差数列的长度.\(n \leq 10000\). [算法]动态规划 两个数之间的差是非常重要的信息,设\(f_{i,j ...
- HDU 3787 A+B 模拟题
解题报告:就是输入两个用逗号隔开的数字,求出这两个数字的和,并且用正常的方式输出来.直接写一个函数将一个包含逗号的数字转换成十进制的数返回就行了.这里推荐一个函数atoi(),参数是char*型的,然 ...
- 第7月第20天 epoll
1. ) { struct sockaddr in_addr; socklen_t in_len; int infd; char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; ...
- 深入浅出js事件
深入浅出js事件 一.事件流 事件冒泡和事件捕获分别由微软和网景公司提出,这两个概念是为了解决页面中事件流(事件发生顺序)的问题. <div id="outer"> & ...
- python学习之——import sys模块
(1)sys.argv sys模块中的argv变量通过使用点号指明——sys.argv——这种方法的一个优势是这个名称不会与任何在你的程序中使用的argv变量冲突.另外,它也清晰地表明了这个名称是sy ...
- java交互方式中的同步与异步
JAVA中交互方式分为同步和异步两种: 1.同步交互:指发送一个请求,需要等待返回,然后才能够发送下一个请求,有个等待过程; 2.异步交互:指发送一个请求,不需要等待返回,随时可以再发送下一个请求,即 ...
- FTP主动/被动原理
FTP 主动模式 1.客户端用大于1024的高位端口发起初始化连接到vsftp服务器的21端口 2.vsftp服务器的21端口主动与客户端大于1024的高位端口建立控制连接 3.vsftp服务器的20 ...
- http和socket之长连接和短连接区别【转】
转自:https://blog.csdn.net/mengyafei43/article/details/25195445 TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层 ...