题目链接:

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(水题)的更多相关文章

  1. 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 ...

  2. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

  3. Xtreme8.0 - Magic Square 水题

    Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...

  4. codeforces #369div2 B. Chris and Magic Square

    题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...

  5. 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 ...

  6. 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 ...

  7. B. Chris and Magic Square

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. bzoj1146整体二分+树链剖分+树状数组

    其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...

  2. Yii路径总结

    如果是 // 就会默认去调 protected/views/layouts //代表 绝对路径 其实 就是 绝对和相对的关系 /代表相对路径,如module/user下的layout 用单斜杠的话默认 ...

  3. ValueStack值栈和ActionContext

    Struts2在OGNL之上提供的最大附加特性就是支持值栈(ValueStack),在OGNL上下文中只能有一个根对象,Struts2的值栈则允许存在许多虚拟对象. 一:值栈(ValueStack) ...

  4. linux 下 整合 nginx 和 php

    一.整合nginx 和 PHP: # vi /usr/local/php/etc/php-fpm.conf 如果该文件不存在,有可能是默认的php-fpm.conf.default 可以使用命令拷贝一 ...

  5. 【iOS】Quartz2D图形上下文

      一.绘图的完整过程 程序启动,显示自定义的view.当程序第一次显示在我们眼前的时候,程序会调用drawRect:方法,在里面获取了图形上下文(在内存中拥有了),然后利用图形上下文保存绘图信息,可 ...

  6. linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解

    最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...

  7. 文科生也能看懂的iptables教程(转载)

    据说还是个MM, 写得很通俗易懂, 还很诙谐, 原文:http://dallascao.com/cn/iptables-tutorial-for-newbies/ 对于斗胆开始玩vps的文科生来讲,i ...

  8. js实现拖拽

    拖拽:最核心是三个事件,鼠标按下,鼠标移动,鼠标弹起.按下时激活拖拽,然后时刻根据鼠标的位置来更新物体的left和top值,达到跟随鼠标的效果,鼠标弹起则取消拖拽. 以下是代码: <!DOCTY ...

  9. CRM 2013 安装前系统和数据库的基础配置

    Win Serer 2012 域控安装参考:http://smallc.blog.51cto.com/926344/1034868  (其中最重要的几步:创建域控(ActiveDirectory域服务 ...

  10. c++类的定义《一》

    最近好忙,一来要在店里看店,二来朋友办结婚酒,搞的我这几天好疲惫啊···博客又有好几天没提笔了. 下午简单看了下书,看到了类的部分,自己动手练习了一下 笔记:1.类是数据类型 / 它的变童就是对象  ...