題目:
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n?×?n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.
Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (
), and the two long diagonals of the grid (the main diagonal —
and the secondary diagonal —
) are equal.![]()
Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?
Input
The first line of the input contains a single integer n (1?≤?n?≤?500) — the number of rows and columns of the magic grid.
n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai,?j (1?≤?ai,?j ≤?10的9次方 or ai,?j?=?0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai,?j will be equal to 0. Otherwise, ai,?j is positive.
It is guaranteed that there is exactly one pair of integers i,?j (1?≤?i,?j?≤?n) such that ai,?j?=?0.
Output
Output a single integer, the positive integer x (1?≤?x?≤?10的18次方) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output ?-1 instead.
If there are multiple solutions, you may print any of them.
Example
Input
3
4 0 2
3 5 7
8 1 6
Output
9
Input
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
Output
1
Input
4
1 1 1 1
1 1 0 1
1 1 2 1
1 1 1 1
Output
-1
Note
In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,
The sum of numbers in each row is:
4?+?9?+?2?=?3?+?5?+?7?=?8?+?1?+?6?=?15.
The sum of numbers in each column is:
4?+?3?+?8?=?9?+?5?+?1?=?2?+?7?+?6?=?15.
The sum of numbers in the two diagonals is:
4?+?5?+?6?=?2?+?5?+?8?=?15.
In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.
題意:在0所在的位置填一個(gè)數(shù)庶骄,使得最后的矩陣每行侨把,每列以及兩條對(duì)角線的和都相等(如果沒(méi)有就輸出-1)。
這道題根據(jù)其他非0行求出0這個(gè)位置的值剖煌,然后遍歷每行每列和兩條對(duì)角線即可祭埂,但要注意一些細(xì)節(jié)填大。
參考代碼:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 500+5;
typedef long long LL;
LL a[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
int loci, locj;
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) {
loci = i;
locj = j;
}
}
}
if (n == 1) {
cout << 1 << endl;
return 0;
}//n = 1時(shí)的特殊情況;
//cout << loci << " " << locj << endl;
bool flag1 = false;
bool flag2 = false;
LL sum1 = 0, sum2 = 0;
for (int i = 1;i <= n;++i) {//主對(duì)角線;
if (a[i][i] == 0) {
flag1 = true;
}
sum1 += a[i][i];
}
//cout << flag1 << " " << sum1 << endl;
for (int i = 1, j = n;i <= n && j >= 1;++i, --j) {//輔對(duì)角線;
if (a[i][j] == 0) {
flag1 = true;
if (i == j) {//0這個(gè)值的位置在主對(duì)角線上;
flag2 = true;
}
}
sum2 += a[i][j];
}
//cout << flag1 << " " << flag2 << " " << sum2 << endl;
if ((!flag1 && sum1 != sum2) || (flag1 && !flag2 && (sum1 == sum2)) || (flag1 && flag2 && sum1 != sum2)) {//不符合條件的幾種情況;
//cout << "xyz" << endl;
cout << -1 << endl;
}
else {//0的位置不在兩條對(duì)角線上且對(duì)角線上的和已經(jīng)相等或0的位置在那兩條對(duì)角線的交叉處且兩條對(duì)角線的和相等或兩條對(duì)角線中有一條上有0;
LL suma = 0;
bool flag = true;
for (int j = 1;j <= n;++j) {//有0的位置的那一行;
suma += a[loci][j];
}
//cout << suma << endl;
LL sumb = 0;
//沒(méi)有0的位置的那一行;
if (loci + 1 <= n) {
for (int j = 1;j <= n;++j) {
sumb += a[loci+1][j];
}
}
else if (loci - 1 >= 1) {
for (int j = 1;j <= n;++j) {
sumb += a[loci-1][j];
}
}
//cout << sumb << endl;
LL sub = sumb - suma;
if (sub == 0) {
cout << -1 << endl;//沒(méi)有補(bǔ)數(shù)就已經(jīng)相等;
return 0;
}
else if (sub < 0) {
cout << -1 << endl;//sub必須大于0;
return 0;
}
a[loci][locj] = sub;//填充0那個(gè)位置的值;
LL sumc, sumd;
for (int i = 1;i <= n;++i) {//每一行求和;
sumc = 0;
for (int j = 1;j <= n;++j) {
sumc += a[i][j];
}
if (sumb != sumc) {
flag = false;
break;
}
}
for (int j = 1;j <= n;++j) {//每一列求和;
sumd = 0;
for (int i = 1;i <= n;++i) {
sumd += a[i][j];
}
if (sumb != sumd) {
flag = false;
break;
}
}
LL sume = 0;
for (int i = 1;i <= n;++i) {//主對(duì)角線求和;
sume += a[i][i];
}
if (sume != sumb) flag = false;
LL sumf = 0;
for (int i = 1, j = n;i <= n && j >= 1;++i, --j) {//輔對(duì)角線求和;
sumf += a[i][j];
}
if (sumf != sumb) flag = false;
if (flag) cout << sub << endl;
else cout << -1 << endl;
}
return 0;
}