CodeForces 711B Chris and Magic Square
简单题。
找一个不存在$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的更多相关文章
- 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这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
- 711B - Chris and Magic Square 模拟
题目大意:在num[i][j]==0处填一个数使每行,每列,对角线的和相同,若果有多种答案输出一种. 题目思路:模拟 #include<iostream> #include<algo ...
- CF 711B - Chris and Magic Square
挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include ...
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- 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 ...
随机推荐
- FineUI Grid 缓存列显示隐藏状态
当列表字段过多时,需要隐藏掉一些,但是再次打开页面又显示出来了,FineUI没有提供缓存功能,那么自己动手,打开[ext-part2.js]找到 “if (Ext.grid.column.RowNum ...
- Sql Server实现多行数据按分组用逗号分隔成一行数据
例如,要将下面的数据 以GROUP_ID进行分组,一组一行,一组中的多个PRODUCT_ID用逗号分隔,select 出来成如下结果: 在Sql Server中,我目前想到的一种方法是写一个函数,如下 ...
- 关于pydev的语法的错误提示
第三方包引入时,eclipse默认会把一些包定为错误的,错误是:“undefined variable from import...” 其实是对的,可是报错,很烦人 解决方法:window -- pr ...
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
iOS开发,最郁闷的莫过于程序毫无征兆地就崩溃了,用bt命令打出调用栈,给出的是一堆系统EXC_BAD_ACCESS的信息,根本没办法定位问题出现在哪里.通常这样的崩溃出现,原因一般就是:调用了已经释 ...
- eclipse在Ubuntu 13.04下的安装过程
eclipse在Ubuntu 13.04下的安装过程及问题小记 一.eclipse安装过程 首先确保在安装eclipse之前已经安装好Java虚拟机 1. eclipse官网下载压缩包 下载地址:ht ...
- Setting up Ubuntu in CoLinux–changing local/keyboard to be English
Today i installed the CoLinux with Ubuntu package, but the default locale setting of the system is G ...
- TPL相关
C#中的Timer System.Windows.Forms.TimerSystem.Threading.Timer System.Timers.Timer 1.System.Windows.Form ...
- 搜索广告与广告网络Demand技术-流式计算平台
流式计算平台-Storm 我们以Storm为例来看流式计算的功能是什么. 下面内容引用自大圆的博客.在Storm中,一个实时应用的计算任务被打包作为Topology发布,这同Hadoop的MapRed ...
- ${pageContext.request.contextPath}的作用
刚开始不知道是怎么回事,在网上也查找了一些资料,看了还是晕. 看了另一个大侠的,终于有了点眉目. 那位大侠在博客中这样写道“然后在网上找,更让我郁闷的事,TMD!网上“抄袭”的真多啊!而且扯了一大堆! ...
- hdu 3074 Multiply game(模板级线段树)
离机房关门还有十分钟,这点时间能干些什么?故作沉思地仰望星空,重新捋一下一天的学习进度,或者,砍掉一棵模板级线段树. 纯模板,就是把单点更新,区间求和改为单点更新,区间求积. 1A. #include ...