codeforces #369div2 B. Chris and Magic Square
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等
题目链接:http://codeforces.com/contest/711/problem/B
分析:题目不难,找到要填的那个数填进去,然后循环比较每行每列以及对角线的和是否相等,题目提交上去卡了几次要注意几点
注意:1.答案数据范围$1\leq x \leq 1e18$,要用 long long
2.特殊情况,$n \equiv 1$时,由于一定有要填的数,所以一定有解
3.反正就是要注意看清楚题目和数据边界情况处理啦
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long mp[][];
long long row[],col[];
int main()
{
int n;
scanf("%d",&n);
memset(row,,sizeof(row));
memset(col,,sizeof(col));
int x,y;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==)
{
x=i;y=j;
}
row[i]+=mp[i][j];
col[j]+=mp[i][j];
}
}
bool is=;
if(x==)
{
mp[x][y]=row[]-row[];
row[x]+=mp[x][y];
col[y]+=mp[x][y];
if(col[]!=row[])
is=;
else
{
for(int i=;i<n;i++)
{
if(row[i]!=row[])
{
is=;break;
}
if(col[i]!=col[])
{
is=;break;
}
}
}
}
else
{
mp[x][y]=row[]-row[x];
row[x]+=mp[x][y];
col[y]+=mp[x][y];
if(col[]!=row[])
is=;
else
{
for(int i=;i<n;i++)
{
if(row[i]!=row[])
{
is=;break;
}
if(col[i]!=col[])
{
is=;break;
}
}
}
}
long long dia1=,dia2=;
for(int i=,j=n-;(i<n&&j>=);i++,j--)
{
dia1+=mp[i][i];
dia2+=mp[i][j];
}
if(dia1!=row[])
is=;
else if(dia2!=dia1)
is=;
if(n==) //注意特判
cout<<""<<endl;
else if(is&&mp[x][y]>=) //注意答案要>=1
cout<<mp[x][y]<<endl;
else
cout<<"-1"<<endl;
return ;
}
codeforces #369div2 B. Chris and Magic Square的更多相关文章
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- 【codeforces 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- 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 C ...
- 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 ...
- 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 ...
- B. Chris and Magic Square
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces 711B - Chris and Magic Square(矩阵0位置填数)
题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...
- 【模拟】Codeforces 711B Chris and Magic Square
题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...
- CodeForces 711B Chris and Magic Square (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
随机推荐
- web学习之Django--安装
前提有python,easy_install 关于easy_install 的安装,比较简单的装法 : wget -q http://peak.telecommunity.com/dist/ez_se ...
- Tomcat重启脚本restart.sh停止脚本stop.sh
Tomcat重启脚本restart.sh停止脚本stop.sh Tomcat本身提供了 startup.sh(启动)shutdown.sh(关闭)脚本,我们在部署中经常会出现死进程形象,无法杀掉进程需 ...
- MVC2,MVC3,MVC4和MVC5的不同
现在MVC的技术日趋成熟,面对着不同版本的MVC大家不免有所迷惑 -- 它们之间有什么不同呢?下面我把我搜集的信息汇总一下,以便大家能更好的认识不同版本MVC的功能,也便于自己查阅. View Eng ...
- System.Configuration引用后ConfigurationManager方法用不了
System.Configuration引用后ConfigurationManager方法却用不了,提示没有引用 需手动添加引用 项目-引用-右击-添加引用-找到System.Configuratio ...
- spring中用到哪些设计模式
1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了: 2.模版模式,这个也很明显,在各种BeanFactory以及ApplicationCon ...
- 我所理解的ECMAScript、DOM、BOM---写给新手们
像很多新手一样,我知道js有三部分组成,即ECMAScript.DOM.BOM三部分组成,ECMAScript是核心解释器.DOM(Document Object Model)是文档对象模型.BOM( ...
- Class.forName()用法及与new区别
平时开发中我们经常会发现:用到Class.forName()方法.为什么要用呢? 下面分析一下: 主要功能Class.forName(xxx.xx.xx)返回的是一个类Class.forName(xx ...
- 在dede:arclist、dede:list等标签中调用附加字段
{dede:list perpage='20'} <div class="f-con01"> <div class="f-con01-l"&g ...
- HQL查询——from子句
HQL查询--from子句 1.from是最基本的HQL语句,from关键字后紧跟持久化类的类名: from Person 表示从Person持久化类中选出全部的实例. 2.推荐为持久化类的每个实例起 ...
- C4.5学习及源码阅读
下面是C4.5源码的下载地址: http://www.rulequest.com/Personal/ 这里先立一个flag,后续更新...... 恕我孤陋寡闻,第一次看到这样的主函数: #includ ...