比赛的时候绝壁打麻烦了。。。

考虑的好麻烦。。。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 【模拟】的更多相关文章

  1. 【模拟】Codeforces 711B Chris and Magic Square

    题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...

  2. CodeForces - 427B (模拟题)

    Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  3. CodeForces - 404B(模拟题)

    Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  4. Codeforces 709B 模拟

    B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  5. codeforces 711B - Chris and Magic Square(矩阵0位置填数)

    题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...

  6. CodeForces - 404A(模拟题)

    Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  7. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  8. Codeforces 452D [模拟][贪心]

    题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...

  9. CodeForces - 796B 模拟

    思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...

随机推荐

  1. sort-list——链表、快慢指针找中间、归并排序

    Sort a linked list in O(n log n) time using constant space complexity. 链表,快慢指针找中点,归并排序. 注意判断条件fast-& ...

  2. IOS自己主动布局中的浮动布局(6)----MyFloatLayout横空出世

    https://github.com/youngsoft/MyLinearLayout 前言 在MyLayout的6大布局中,每种布局都有不同的应用场景. 且每种布局的子视图的约束机制不一样:线性布局 ...

  3. android项目笔记(一)

    1.getInstance:单例模式创建类的实例,getInstance在单例模式(保证一个类仅有一个实例,并提供一个访问它的全局访问点)的类中常见,用来生成唯一的实例,getInstance往往是s ...

  4. spring MVC使用Interceptor做用户登录判断

    在任何一个项目中,我们必须要用到的就是用户登录,那么就少不了用户是否登录的判断,如果我们每一个请求都要去做一次判断,那么就会变得很麻烦,但我们复制粘贴的时候我们就要考虑我们的代码写的是不是有问题,是不 ...

  5. SpringBoot学习之文件结构和配置文件

    Springboot文件结构和配置文件 转载:http://www.zslin.com/web/article/detail/11 项目文件结构 新建的Springboot项目的文件结构如下: |-c ...

  6. chrome.declarativeWebRequest

    chrome.declarativeWebRequest 清单文件 规则 条件与操作的求值 使用优先级覆盖规则 类型 HeaderFilter RequestMatcher CancelRequest ...

  7. 关于 truncate table 的一点学习札记

    ---下面整理笔记来之 itpub 的各位前辈的语录.这里做了一个汇总.仅供学习. truncate table后,oracle会回收表和其表中所在的索引到initial 大小,也就是初始分配的seg ...

  8. MFC 的 Picture Control 加载 BMP/PNG 图片的方法

    1. 加载 BMP CStatic* pWnd = (CStatic*)GetDlgItem(IDC_PIC); // 得到 Picture Control 句柄 pWnd->ModifySty ...

  9. 状态模式-State

    状态模式:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式结构图: 代码实现:

  10. Spring中的事务管理(学习笔记)

    什么是事物? 事物是指逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 事物的特性: 原子性.一致性.隔离性.持久性 Spring事务管理的高级接口: PlatformTransactionMa ...