CodeForces 711BChris and Magic Square
来源:互联网 发布:网卡mac地址怎么恢复 编辑:程序博客网 时间:2024/06/03 19:51
http://codeforces.com/problemset/problem/711/B
给一个n*n的矩阵,将其中一个0改成其他正数,使得每行每列2个对角线的和都相等,暴力即可,但是要注意如果得到的答案是一个负数,那么也算是不符合的情况。
#include<bits/stdc++.h>using namespace std;long long int a[555][555];long long int b[555];long long int c[555];int main(){int n;long long int zx;long long int yx;while(scanf("%d",&n)!=EOF){zx=0;yx=0;int i,j;int u,v;memset(b,0,sizeof(b));memset(c,0,sizeof(c));for(i=1;i<=n;i++){for(j=1;j<=n;j++) { scanf("%I64d",&a[i][j]); b[i]+=a[i][j]; c[j]+=a[i][j]; if(a[i][j]==0) { u=i;v=j;}if(i==j)zx+=a[i][j];if(i==n+1-j)yx+=a[i][j];}}if(n==1){cout<<"1"<<endl;continue;}else {bool flag=0;long long int th;if(u==1){th=b[2]-b[1];if(th<0)flag=1;b[u]+=th;c[v]+=th;if(u==v)zx+=th;if(u+v==n+1)yx+=th;}else {th=b[u-1]-b[u];if(th<0)flag=1;b[u]+=th;c[v]+=th;if(u==v)zx+=th;if(u+v==n+1)yx+=th;}if(zx!=yx)flag=1;for(i=2;i<=n;i++){if(b[i]!=b[i-1])flag=1;if(c[i]!=c[i-1])flag=1;}if(zx!=b[1])flag=1;if(flag==0&&th>0)cout<<th<<endl;else cout<<"-1"<<endl;}}return 0;}
阅读全文
0 0
- CodeForces 711BChris and Magic Square
- Codeforces 711B Chris and Magic Square
- Codeforces 711B Chris and Magic Square
- CodeForces 711B - Chris and Magic Square
- Codeforces 711B- Chris and Magic Square
- Codeforces 711 B Chris and Magic Square
- Chris and Magic Square CodeForces
- codeforces 711B. Chris and Magic Square【模拟】
- Codeforces Problem 711B Chris and Magic Square(constructive algorithms)
- CodeForces 711B. Chris and Magic Square(水题)
- 【模拟】Codeforces 711B Chris and Magic Square
- codeforces#369Div2(711B)Chris and Magic Square
- codeforces 711 B. Chris and Magic Square (简单)
- CodeForces 711B Chris and Magic Square(找规律)
- codeforces B. Chris and Magic Square
- 【Codeforces Round #369 (Div. 2)】Codeforces 711B Chris and Magic Square
- 模拟-711B - Chris and Magic Square
- Coderforces 711B Chris and Magic Square
- javascript Date format(js日期格式化)
- 链表
- 内置函数 & UDF函数在Hive中的使用
- 计算机图形学(1)——概论
- 学习某些 API 的方法
- CodeForces 711BChris and Magic Square
- Flask应用实例
- 51nod 1710 复杂度分析2
- Redis学习笔记
- VimFx,让firefox支持类Vim编辑器的快捷操作
- 卷积的理解
- CodeForces 760BFrodo and pillows
- 对贝叶斯理解以及解释贝叶斯函数
- Spring 自动装配