codeforces 711B B. Chris and Magic Square(水题)
题目链接:
题意:
问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=4e5+10;
const int maxn=1e3+520;
const double eps=1e-12; LL mp[505][505],l[505],r[505];
int main()
{
int n,x=0,y=0;
read(n);
For(i,1,n)
{
r[i]=0;
For(j,1,n)
{
read(mp[i][j]);
if(mp[i][j]==0)
{
x=i;y=j;
}
r[i]+=mp[i][j];
}
}
if(n==1)
{
cout<<"1\n";
return 0;
}
int num=0;
for(int i=2;i<=n;i++)
{
if(r[i]!=r[i-1])num++;
}
if(num>2)cout<<"-1\n";
else
{
LL temp;
if(x>1)temp=r[1];
else temp=r[2];
LL sum=temp;
for(int i=1;i<=n;i++)
{
if(mp[x][i])temp-=mp[x][i];
}
mp[x][y]=temp;
if(temp<=0)cout<<"-1\n";
else
{
for(int i=1;i<=n;i++)
{
l[i]=0;
for(int j=1;j<=n;j++)
{
l[i]+=mp[j][i];
}
}
for(int i=2;i<=n;i++)
{
if(l[i]!=sum)
{
cout<<"-1\n";
return 0;
}
}
temp=0;
for(int i=1;i<=n;i++)
{
temp+=mp[i][i];
}
if(temp!=sum)
{
cout<<"-1\n";
return 0;
}
temp=0;
for(int i=1;i<=n;i++)
{
temp+=mp[i][n-i+1];
}
if(temp!=sum)
{
cout<<"-1\n";
return 0;
}
cout<<mp[x][y]<<endl;
}
} return 0;
}
codeforces 711B B. Chris and Magic Square(水题)的更多相关文章
- 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 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- Xtreme8.0 - Magic Square 水题
Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...
- codeforces #369div2 B. Chris and Magic Square
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...
- 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 Gym 100187L L. Ministry of Truth 水题
L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- Codeforces Round #185 (Div. 2) B. Archer 水题
B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...
随机推荐
- css样式reset
初始化样式. @CHARSET "UTF-8"; body,ur,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div, ...
- svn的管理与维护要点—纯手工编写
由于在公司要维护阿里云的linux服务器,我们的svn服务器就安在阿里云上面.所以经常会涉及到svn的维护操作.离职的时候编写交接文档,刚好有充足的时间写一篇说明介绍,此说明纯原创,不是从网上复制,手 ...
- VM虚拟机忘记密码
关掉虚拟机. VM->Settings,选中Hard Disk,在右边出现了Utilities的一个下拉栏,OK,点击它选择Map,这时弹出一个"Map Virtual Disk&qu ...
- ArrayList 与 LinkedList
ArrayList:数组结构,插入删除的效率低,查询的效率较高. LinkedList:链接数据结构,插入删除的效率较高,查询的效率低. 两者的使用 ArrayList:适合用作添加数据,做查询. L ...
- python基础之正则表达式。
简介 就其本质而言,正则表达式是内嵌在python内,由re模块实现,小型的专业化语言,最后由c写的匹配引擎执行.正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来 ...
- SharePoint 2010 文档管理系列
前言,这是自己第一次写一个系列的文档,本来想使用SharePoint 2013版本,但是碍于SharePoint 2013对于硬件要求过高,自己的笔记本无法承受,所以退而求其次选择了在SharePoi ...
- Apache服务器的URL重定向
前端时间要整个Apache重定向功能,在此记录一下. 一.安装Apache Windows版本官方下载安装文件httpd-2.2.21-win32-x86-openssl-0.9.8r,选择安装目录, ...
- Objective-C 理解之方括号[ ]的使用
1,用于通知某个对象该做什么,即通知对象去执行某动作. 在Objective-c中,“[]”的意义:它们用于通知某个对象该做什么.这种通知对象执行某中操作的行为被称为“发送消息”. 例如: [sh ...
- 发布的时候Archive灰色
主要是因为发布的时候要选为将模拟器选择一下,选为Generic iOS Device. 来自为知笔记(Wiz)
- WPF + Caliburn.Micro +ActionMessage事件绑定
ActionMessage事件绑定是个人觉的算是CM的精髓了,比如说我在View里面放个button,我们要在他的click事件里面写东西,怎么写.如果是WPF我们直接在CS里面写就可以.但是CM不行 ...