Codeforces 711B 【模拟】
比赛的时候绝壁打麻烦了。。。
考虑的好麻烦。。。wa7。。。还要判断出来的是不是positive的。。。
好吧。。认了。。
#include<cstdio>
#include <map>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int N=5e2+10;
LL ma[N][N];
LL row[N];
LL col[N];
LL s1,s2;
int n;
int main()
{
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
s1=s2=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%I64d",&ma[i][j]);
//1的话随便
if(n==1)
{
if(!ma[1][1])
puts("1");
else
printf("%I64d\n",ma[1][1]);
return 0;
}
//找到0位置
int x=-1,y=-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(!ma[i][j])
{
x=i;
y=j;
break;
}
if(x!=-1)
break;
}
//printf("%d %d\n",x,y);
for(int i=1;i<=n;i++)
{
if(i!=x)
{
for(int j=1;j<=n;j++)
row[i]+=ma[i][j];
}
}
for(int j=1;j<=n;j++)
{
if(j!=y)
{
for(int i=1;i<=n;i++)
col[j]+=ma[i][j];
}
}
//斜的
//顺的
if(x!=y)
{
for(int i=1;i<=n;i++)
{
s1+=ma[i][i];
}
}
//逆的
if(x+y!=n+1)
for(int i=1;i<=n;i++)
{
s2+=ma[i][n-i+1];
}
if(x!=y&&x+y!=n+1)
if(s1!=s2)
{
puts("-1");
return 0;
}
//横的
LL temp1,temp2;
for(int i=1;i<=n;i++)
{
if(i!=x)
{
temp1=row[i];
break;
}
}
for(int i=1;i<=n;i++)
{
if(i!=x)
{
if(temp1!=row[i]||(s1!=row[i]&&x!=y)||(s2!=row[i]&&x+y!=n+1))
{
//printf("here?\n");
puts("-1");
return 0;
}
}
}
//竖的
for(int i=1;i<=n;i++)
{
if(i!=y)
{
temp1=col[i];
break;
}
}
for(int i=1;i<=n;i++)
{
if(i!=y)
{
if(temp1!=col[i]||(x!=y&&s1!=col[i])||(s2!=col[i]&&x+y!=n+1))
{
puts("-1");
return 0;
}
}
}
temp2=temp1;
//求一个
//printf("temp1=%I64d\n",temp1);
for(int i=1;i<=n;i++)
{
if(i==x)
{
for(int j=1;j<=n;j++)
{
if(ma[i][j])
temp1-=ma[i][j];
}
ma[x][y]=temp1;
break;
}
}
LL k1,k2,k3,k4;
temp1=temp2;
k1=0;
k2=0;
k3=0;
k4=0;
for(int i=1;i<=n;i++)
k1+=ma[x][i];
for(int i=1;i<=n;i++)
k2+=ma[i][y];
if(k1!=temp1||k2!=temp2||k1!=k2)
{
//printf("here?\n");
puts("-1");
return 0;
}
if(x==y)
{
for(int i=1;i<=n;i++)
k3+=ma[i][i];
if(k3!=k2)
{
puts("-1");
return 0;
}
}
if(x+y==n+1)
{
for(int i=1;i<=n;i++)
k4+=ma[i][n+1-i];
if(k4!=k2)
{
puts("-1");
return 0;
}
}
if(ma[x][y]>0)
printf("%I64d\n",ma[x][y]);
else
puts("-1");
return 0;
}
/*
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
*/
Codeforces 711B 【模拟】的更多相关文章
- 【模拟】Codeforces 711B Chris and Magic Square
题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...
- CodeForces - 427B (模拟题)
Prison Transfer Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- CodeForces - 404B(模拟题)
Marathon Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- Codeforces 709B 模拟
B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- codeforces 711B - Chris and Magic Square(矩阵0位置填数)
题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- Codeforces 390A( 模拟题)
Inna and Alarm Clock Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- Codeforces 452D [模拟][贪心]
题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...
- CodeForces - 796B 模拟
思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...
随机推荐
- win10中显示wpcap.dll丢失的处理方式
win10中显示wpcap.dll丢失的处理方式 学习了:https://jingyan.baidu.com/article/4f34706e30e673e387b56dd8.html 直接安装Win ...
- UVA 11246 - K-Multiple Free set(数论推理)
UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合.求一个子集合.使得元素个数最多,而且不存在有两个元素x1 * k = x2,求出最多的元素个数是 ...
- iOS设计模式 - (3)简单工厂模式
iOS设计模式 - (3)简单工厂模式 by Colin丶 转载请注明出处: http://blog.csdn.net/hitwhylz/article/ ...
- Pycharm下运行程序查看每个变量的值的方法(类似于Spyder和MATLAB)
昨天,用了大量篇幅讲了Spyder的各种问题,之所以要用Spyder,最重要的一个原因就是能够非常方便的查看中间变量的值.类似MATLAB的工作空间,非常方便.如下图所示: 但是Spyder的代码自动 ...
- NPOI实现Excel导入
导入功能实现: ]; GetExtensionsFromFileStream(file.InputStream); using NPOI.XSSF.UserModel; public List< ...
- 正则工具类以及FinalClass
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/27811103 完整版见https://jadyer. ...
- JS是否确认提示 +页面跳转
JS友好提示 +页面跳转 function logout()...{if (confirm("你确定要注销身份吗?是-选择确定,否-选择取消"))...{window.locati ...
- 谈谈Paxos一致性算法和一致性这个名词
转自:http://www.cnblogs.com/esingchan/p/3917718.html 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的& ...
- android checkbox radiogroup optionmenu dialog
\n换行 UI visible:View.INVISIBLE 不可见,占用空间,View.GONE 不可见,不占用空间 菜单 res右击新建menu xml 自动新建menu文件夹 context ...
- vue 中的组件通信
vue中组件通信,一般分为三种情况,父与子,子与父,子子之间. 一.父与子通信 父组件将值传给子组件,一般通过props,设置默认的类型.调用的时候通过 xx=" ", 或者:XX ...