简单题。

找一个不存在$0$的行,计算这行的和(记为$sum$),然后就可以知道$0$那个位置应该填的数字(记为$x$)。

如果$x<=0$,那么无解,否则再去判断每一行,每一列以及两个斜对角的和是否均为$sum$。需要注意的是$n=1$的时候,直接输出$1$就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-; const int maxn=;
int n;
LL a[maxn][maxn];
int r,c; int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%lld",&a[i][j]);
if(a[i][j]==) r=i, c=j;
}
} if(n==) { printf("1\n"); } else
{
int pos=-;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
if(i!=r) { pos=i; break; }
if(pos!=-) break;
} LL sum=; for(int j=; j<=n; j++) sum=sum+a[pos][j];
LL sum2=; for(int j=; j<=n; j++) sum2=sum2+a[r][j];
a[r][c]=sum-sum2;
if(a[r][c]<=) printf("-1\n");
else
{
bool fail=;
for(int i=; i<=n; i++)
{
LL t=; for(int j=; j<=n; j++) t=t+a[i][j];
if(t!=sum) fail=;
} for(int j=; j<=n; j++)
{
LL t=; for(int i=; i<=n; i++) t=t+a[i][j];
if(t!=sum) fail=;
} LL t=;
for(int i=; i<=n; i++) t=t+a[i][i];
if(t!=sum) fail=; t=; for(int i=; i<=n; i++) t=t+a[i][n-i+];
if(t!=sum) fail=; if(fail) printf("-1\n");
else printf("%lld\n",a[r][c]);
}
}
return ;
}

CodeForces 711B Chris and Magic Square的更多相关文章

  1. codeforces 711B - Chris and Magic Square(矩阵0位置填数)

    题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...

  2. 【模拟】Codeforces 711B Chris and Magic Square

    题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...

  3. CodeForces 711B Chris and Magic Square (暴力,水题)

    题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...

  4. 711B - Chris and Magic Square 模拟

    题目大意:在num[i][j]==0处填一个数使每行,每列,对角线的和相同,若果有多种答案输出一种. 题目思路:模拟 #include<iostream> #include<algo ...

  5. CF 711B - Chris and Magic Square

    挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include ...

  6. codeforces 711B B. Chris and Magic Square(水题)

    题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...

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

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

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

随机推荐

  1. 如何判断一个对象是否为jquery对象

    当我们在用jquery的each做循环遍历的时候常常会使用到this 而有时候我们不知道this所指的到底是什么,因为要使用jquery 的方法 前提此对象必须是jquery对象. 另外要判断一个ja ...

  2. jQuery判断浏览器类型

    if ($.browser.msie) { alert("IE浏览器"); } else if ($.browser.opera) { alert("opera浏览器&q ...

  3. Koala Framework

    Koala Framework是什么?我为什么要写这个框架?   当时的监管组,技术力量累积的很少,还在直连DB,使用着DataTable.DataSet作为数据的承载,监管是公司最近几年主推的项目, ...

  4. How to use USB 3G dongle/stick Huawei E169/E620/E800 ( Chip used Qualcomm e1750) in Linux (China and world)

    Using this 3G module in Linux is so great. I want it. So I made it. The 3G dongle of Huawei E169/E62 ...

  5. 探讨C++ 变量生命周期、栈分配方式、类内存布局、Debug和Release程序的区别

    探讨C++ 变量生命周期.栈分配方式.类内存布局.Debug和Release程序的区别(一) 今天看博客园的文章,发现博问栏目中有一个网友的问题挺有趣的,就点进去看了下,标题是“C++生存期问题”,给 ...

  6. PHP, Python Nginx works together!

    Nginx is so good at delivering requests to many others. Good! Now let's use the nginx upstream modul ...

  7. 在gem5的full system下运行 x86编译的测试程序 running gem5 on ubuntu in full system mode in x86

    背景 上篇博客写了如何在gem5的full system模式运行alpha的指令编译的程序,这篇博客讲述如何在gem5的full system模式运行x86指令集编译的程序,这两种方式非常类似. 首先 ...

  8. [ios-必看] iOS 下实现解压缩

    http://blog.csdn.net/lyy_whg/article/details/11971581 http://blog.sina.com.cn/s/blog_833996210100udk ...

  9. ColumnEdit 数据源修改

    应用场景 当从ColumnEdit(如SearchLookUpEdit)中选取一条记录后,ColumnEdit的数据源不再出现这条记录.效果图如下 选择前 选择一条记录后,上一条记录不再显示. 此处是 ...

  10. 伪 alter 弹窗 +弹窗统一

    你应该有遇到这种情况:alert() 在手机上面显示的时候 反正是不好看  有的时候就只看到一个白色的框  看不到提示信息  反正很反人类  这时候我觉得 马上会有产品跟你说 这个要改掉. 然后我就遇 ...