CodeForces 711B - Chris and Magic Square

//http://codeforces.com/problemset/problem/711/B
//又打了一场掉分赛 这个题最后一定要判断是否小于0 不然会wa7

#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <functional>
#include <vector>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
int main()
{
cin.sync_with_stdio(false);
long long A[505][505]= {0};
int N,X,Y;
cin>>N;
for (int i=1; i<=N; i++)
for (int j=1; j<=N; j++)
{
cin>>A[i][j];
if (A[i][j]==0)
X=i,Y=j;
}
if (N==1)
{
cout<<1;
return 0;
}
long long sum=0;
bool ok=true;
for (int i=1; i<=N; i++)
{
if (i==X)
continue;
for (int j=1; j<=N; j++)
sum+=A[i][j];
break;
}
for (long long i=1,x=0; i<=N; i++,x=0)
{
if (i==X)
continue;
for (int j=1; j<=N; j++)
x+=A[i][j];
if (x!=sum)
ok=false;
}
for (long long j=1,x=0; j<=N; j++,x=0)
{
if (j==Y)
continue;
for (int i=1; i<=N; i++)
x+=A[i][j];
if (x!=sum)
ok=false;
}
long long x=0;
if (!ok)
cout<<-1;
else
{
x=0;
for (int j=1; j<=N; j++)
x+=A[X][j];
A[X][Y]=sum-x;
x=0;
for (int i=1; i<=N; i++)
x+=A[i][Y];
if (x!=sum)
ok=false;
x=0;
for (int i=1; i<=N; i++)
x+=A[i][i];
if (x!=sum)
ok=false;
x=0;
for (int i=1; i<=N; i++)
x+=A[i][N+1-i];
if (x!=sum)
ok=false;
if (!ok)
cout<<-1;
else
{
if (A[X][Y]<=0)
cout<<-1;
else
cout<<A[X][Y];
}
}
return 0;
}


CodeForces - 711B Chris and Magic Square

2017-09-03 21:11:59

Codeforces 711B Chris and Magic Square

2016-08-30 10:16:48

Chris and Magic Square(CodeForces - 711B)

2018-03-30 09:37:24

Chris and Magic Square CodeForces - 711B

2017-03-22 20:50:39

CodeForces 711B Chris and Magic Square

2017-10-14 19:28:25

Coderforces 711B Chris and Magic Square

2016-08-30 13:19:20

codeforces#369Div2(711B)Chris and Magic Square

2016-08-30 21:03:22

模拟-711B - Chris and Magic Square

2016-08-30 12:56:32

C - Chris and Magic Square CodeForces - 711B

2017-10-15 10:57:50

【模拟】Codeforces 711B Chris and Magic Square

2016-08-30 14:51:23