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 ...
随机推荐
- 跟着实例学习设计模式(7)-原型模式prototype(创建型)
原型模式是创建型模式. 设计意图:用原型实例指定创建对象的类型,并通过拷贝这个原型来创建新的对象. 我们使用构建简历的样例的类图来说明原型模式. 类图: 原型模式主要用于对象的复制.它的核心是就是类图 ...
- 一起来当网管(一)——Windows Server上的DHCP配置
学校实验室里大大小小设备还不少,网络环境虽说不复杂,但也比家用的复杂一些.就当练练手吧,刚好写点文章,免得以后实验室网络没人管了.那么就先从DHCP的配置来讲吧! 1.DHCP是什么.有什么用 DHC ...
- 翻译:A Tutorial on the Device Tree (Zynq) -- Part III
A Tutorial on the Device Tree (Zynq) -- Part III 定义外设 可能你读本文是为了给你的设备写一个Linux驱动,在这方面要推荐著名的<Linux D ...
- A new session could not be created. (Original error: Requested a new session but one was in progress) )错误解决办法
z在desiredCapabilities里新增这俩居然fix了问题,原因暂时不得而知: capabilities.setCapability("unicodeKeyboard", ...
- Android GUI系统学习1:Gralloc
Gralloc模块是从Android Eclair(android 2.1)開始增加的一个HAL模块,Gralloc的含义为是Graphics Alloc(图形分配).他对上为libui提供服务,为其 ...
- MRUnit测试
新建一个专门的测试类,代码如下: wordcount的map函数输入string line, 输出<单词 , 1> 右键-> run as junit 出错了,因为输出不是 ...
- numpy计算
import numpy as np import cv2 from PIL import Image #lenna.jpg # Create a black image #img=np.zeros( ...
- python 2: 解决python中的plot函数的图例legend不能显示中文问题
问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢? 解决: plt.figure() plt.title(u'训练性能', fontproperties=f ...
- Java类加载器(ClassLoader)
类加载的机制的层次结构 每个编写的”.java”拓展名类文件都存储着需要执行的程序逻辑,这些”.java”文件经过Java编译器编译成拓展名为”.class”的文件,”.class”文件中保存着Jav ...
- js遍历map
//火狐控制台打印输出: Object { fileNumber="文件编号", fileName="文件名称"} console.log(map); for( ...