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这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
随机推荐
- Texture tiling and swizzling
Texture tiling and swizzling 原帖地址:http://fgiesen.wordpress.com If you’re working with images in your ...
- Razor语法&ActionResult&MVC
Razor代码复用 mvc 4 razor语法讲解和使用 了解ASP.NET MVC几种ActionResult的本质:EmptyResult & ContentResult 了解ASP.NE ...
- nginx auth
location / { auth_basic "closed site";##Context:http, server, location, limit_except auth_ ...
- IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容
IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容 UIAlertController *alertVC = [UIAlertController alertControl ...
- 使用JDOM操作XML
JDOM介绍 JDOM是使用Java语言编写的,用于读写及操作XML的一套组件,Jdom同时具有DOM修改文件的优点和SAX读取快速的优点. JDOM的使用 首先下载JDOM的JAR包,本文使用的是j ...
- Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)
Kafka 0.9版本对java client的api做出了较大调整,本文主要总结了Kafka 0.9在集群搭建.高可用性.新API方面的相关过程和细节,以及本人在安装调试过程中踩出的各种坑. 关于K ...
- OC-《购票系统》
来个命令行的购票系统 --1-- 需求分析 1.1 分析 1.2 功能分析 1.3 流程分析 --2-- 原型展示 2.1 界面原型 --3-- 系统设计 3.1 类设计 3.2 框架模块设计 --4 ...
- Icon字体制作
工具网站:https://icomoon.io 这个网站是免费的 功能也很强大 不过我那是铁通网络,导致这个网站不能访问,做vpn跳转就可以了 正题: 点击这里进入制作页面: 点击这里选取 svg 矢 ...
- Python Locust对指定网站“一键压测”
[本文出自天外归云的博客园] 前篇 前篇:Python Locust性能测试框架实践 本篇 承上——归纳过程 在前篇的基础上,我们可以利用Locust性能测试框架编写python脚本对指定网站或者接口 ...
- Python 对目录中的文件进行批量转码(GBK>UTF8)
通过python实现对文件转码,其实处理很简单: 1.打开读取文件内容到一个字符串变量中,把gbk编码文件,对字符串进行decode转换成unicode 2.然后使用encode转换成utf-8格式. ...