zsd

# codeforces711b

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long map[510][510];
long long sum = 0;
int main()
{
int  i , j ,n ,x , y , xn = 0, xy = 0, yn = 0 , yy = 0;
scanf("%d",&n);
if(n == 1){
printf("1\n");
return 0;
}
for( i = 1 ; i <= n ; i++){
for( j = 1 ; j <= n ; j++){
scanf("%d",&map[i][j]);
//     getchar();
//       if(map[i][j] == '0')
//          printf("***\n"),x = j , y = i;
}
}
for( i = 1 ; i <= n ; i++){
for( j = 1 ; j <= n ; j++){
if(map[i][j] == 0){
y = i , x = j;
}

}
}
//  printf("x = %d , y = %d\n",x , y);
if((x - 1) >= 1) xn = 1;
if((x + 1) < n + 1) xy = 1;
if((y - 1) >= 1) yn = 1;
if((y + 1) < n + 1) yy = 1;
if(yn){
for( i = 1 ; i <= n ; i++)
sum += map[y - 1][i];
}
else{
for( i = 1 ; i <= n ; i++)
sum += map[y + 1][i];
}
long long num  = sum , sum1;
for(int i = 1 ; i <= n ; i++){
num -= map[y][i];
}
map[y][x] = num;
//printf("num = %d\n",num);
int flag = 1;
for( i = 1 ; i <= n ; i++){
sum1 = 0;
for( j = 1 ; j <= n ; j++){
sum1 += map[i][j];
}
// printf("%d\n",sum1);
if(sum1 != sum){
printf("-1\n");
return 0;
}
}
for( i = 1 ; i <= n ; i++){
sum1 = 0;
for( j = 1 ; j <= n ; j++){
sum1 += map[j][i];
}
if(sum1 != sum){
printf("-1\n");
return 0;
}
}
sum1 = 0;
for( i = 1 ; i <= n ; i++)
sum1 += map[i][i];
if(sum1 != sum){
printf("-1\n");
return 0;
}
sum1 = 0;
for( i = 1 ; i <= n ; i++){
sum1 += map[i][n + 1 - i];
}
if(sum1 != sum){
printf("-1\n");
return 0;
}
if(map[y][x] > 0)
printf("%I64d\n",map[y][x]);
else
printf("-1\n");
return 0;
}

#### 【codeforces711B】Chris and Magic Square

2016-09-11 22:23:44

#### 【codeforces711B】Bus to Udayland

2016-09-11 22:05:58

#### codeforces711B Chris and Magic Square 简单枚举

2016-08-30 22:15:10