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 ...
随机推荐
- firefox os 开发模拟器1.4版本号安装开发具体解释
首先在使用firefox os 模拟器的时候必须先下载firefox 浏览器,这个是众多web开发人员必备的工具,下载地址firefox 浏览器 .在最新的官方版本号是1.5版的模拟器,可是如今还不是 ...
- grunt 试用笔记
Gruntjs是JavaScript项目的构建工具,也是基于node的一个命令行工具.很多开源JS项目都是使用它搭建.如jQuery.Qunit.CanJS等.它有以下作用 合并JS文件压缩JS文件单 ...
- mysql 查询 优化
1.基本原则:优化数据访问 (1)是否想服务器请求了不需要的数据?提取超过需要的列,多表连接时提取所有列,提取所有列都会消耗不必要的资源,提取你所需要的列就可以了. (2)MySQL检查了太多的数据吗 ...
- Arcgis Engine(ae)接口详解(4):featureClass的feature插入
//由于测试数据不完善,featureClass在此要只设null值,真实功能要设实际的值 IFeatureClass featureClass = null; //获取某个字段的索引,后面取字段值用 ...
- (testng多个class文件执行时混乱,不是等一个class内的所有methods执行完再去执行下一个class内的内容)问题的解决
问题描述如下: We use TestNG and Selenium WebDriver to test our web application. Now our problem is that we ...
- H5新增表单属性
一.form属性 <form id="test"> <input type="text" placeholder="请输入合适的信息 ...
- 编程算法 - 数组中出现次数超过一半的数字 代码(C)
数组中出现次数超过一半的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 数组中有一个数字出现的次数超过数组长度的一半, 请找出这个数字. ...
- Windows 8实用窍门系列:20.Windows 8中的GridView使用(二)和DataTemplateSelector
在本文中所讲述内容的实例仍然沿用于上篇文章,有什么疑惑可以参考上篇文章. 一 GroupStyle 在GridView控件中我们可以对数据进行分组显示,通过对GridView的GroupStyle进行 ...
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
- CSS 媒体类型 总结
CSS 媒体类型 媒体类型允许你指定文件将如何在不同媒体呈现.该文件可以以不同的方式显示在屏幕上,在纸张上,或听觉浏览器等等. 媒体类型 一些CSS属性只设计了某些媒体.例如"voice-f ...