【模拟】Codeforces 711B Chris and Magic Square
题目链接:
http://codeforces.com/problemset/problem/711/B
题目大意:
N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数。无解输出-1。幻方是每一行每一列和两条主对角线的和都相等。
题目思路:
【模拟】
题目没看清外加爆intWA了好多次。。罪过。
求出每一行每一列和对角线的和,为0的那个用随意一行的和扣去0的那一行就可以得到。只要验证其余的是否都相等即可。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 504
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
LL a[N][N];
LL sum[N],tot[N];
int sx,sy;
bool judge()
{
int i,j;
LL ll,c;
if(sx==)
a[sx][sy]=sum[]-sum[sx];
else a[sx][sy]=sum[]-sum[sx];
sum[sx]+=a[sx][sy],tot[sy]+=a[sx][sy];
c=sum[];
for(i=;i<=n;i++)
if(sum[i]!=c)
return ;
for(j=;j<=n;j++)
if(tot[j]!=c)
return ;
for(i=,ll=;i<=n;i++)
ll+=a[i][i];
if(ll!=c)return ;
for(i=,ll=;i<=n;i++)
ll+=a[i][n+-i];
if(ll!=c)return ;
return ;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k; // for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
mem(sum,);mem(tot,);
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
scanf("%I64d",&a[i][j]);
if(a[i][j]==)sx=i,sy=j;
sum[i]+=a[i][j];
tot[j]+=a[i][j];
}
}
if(n==)
{
puts("");
continue;
}
if(judge() && a[sx][sy]>)printf("%I64d\n",a[sx][sy]);
else puts("-1");
}
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 (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
- CodeForces 711B Chris and Magic Square
简单题. 找一个不存在$0$的行,计算这行的和(记为$sum$),然后就可以知道$0$那个位置应该填的数字(记为$x$). 如果$x<=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 ...
随机推荐
- 启动hadoop时候报错:localhost: ssh: Could not resolve hostname localhost: Temporary failure in name resolution”
这个错误是由于配置文件没有配置好.解决方案如下: 1 打开profile文件 vim /etc/profile 2 在文件最后加入的内容应该如下(高亮的两句一般是大家缺少的): export JAVA ...
- img标签块状与内联的博弈
新手,请前辈们不吝赐教 说到html中img标签是内联还是块状元素,我们首先要知道什么是内联(inline),什么又是块状(block)? 我也在网上查看了一些别人分享的经验,有一个讲到了文档流的概念 ...
- ORACLE每组只保留一条记录
删除同一组内其他记录 DELETE from memactivities a where exists(select 1 FROM (select Uuid,ci_no,lst_upd_ts,ROW_ ...
- 关于Android4.x系统默认显示方向各种修改
1.设置属性值 在device.mk文件中加入PRODUCT_PROPERTY_OVERRIDES += \ ro.sf.hwrotation=180 2.设置屏幕默认显示方向 在frameworks ...
- WCF,WebAPI,WCFREST和WebService的区别
Web ServiceIt is based on SOAP and return data in XML form.It support only HTTP protocol.It is not o ...
- 【POJ1151】【扫描线+线段树】Atlantis
Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...
- 第八篇、SVN在Mac上使用
Mac自带svn软件 1.创建目录 svn-repository/source-code 2.svnadmin create /Users/liaokailin/svn-repository/sour ...
- sed 简明教程
做个标记 http://coolshell.cn/articles/9104.html sed全名叫stream editor,流编辑器,用程序的方式来编辑文本,相当的hacker啊.sed基本上就是 ...
- date日期比较和格式化方法
时间的比较 var now = new Date();//现在时间 var setDate = new Date(2017,0,5,17,55,55);//设定的时间 laert(now < s ...
- form WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法
针对WebBrowser控件中自动点击弹出框及禁用脚本提示问题得到如下几种实际情况的解决办法,绝对管用. 1.屏蔽弹出错误脚本 将WebBrowser控件ScriptErrorsSuppressed设 ...