CodeForces 711B Chris and Magic

2016-12-11  本文已影响56人  科学旅行者

题目:

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所在的位置填一个数,使得最后的矩阵每行,每列以及两条对角线的和都相等(如果没有就输出-1)。
这道题根据其他非0行求出0这个位置的值,然后遍历每行每列和两条对角线即可,但要注意一些细节。

参考代码:

#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时的特殊情况;
    //cout << loci << " " << locj << endl;
    bool flag1 = false;
    bool flag2 = false;
    LL sum1 = 0, sum2 = 0;
    for (int i = 1;i <= n;++i) {//主对角线;
        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) {//辅对角线;
        if (a[i][j] == 0) {
            flag1 = true;
            if (i == j) {//0这个值的位置在主对角线上;
                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的位置不在两条对角线上且对角线上的和已经相等或0的位置在那两条对角线的交叉处且两条对角线的和相等或两条对角线中有一条上有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;
        //没有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;//没有补数就已经相等;
            return 0;
        }
        else if (sub < 0) {
            cout << -1 << endl;//sub必须大于0;
            return 0;
        }
        a[loci][locj] = sub;//填充0那个位置的值;
        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) {//主对角线求和;
            sume += a[i][i];
        }
        if (sume != sumb) flag = false;
        LL sumf = 0;
        for (int i = 1, j = n;i <= n && j >= 1;++i, --j) {//辅对角线求和;
            sumf += a[i][j];
        }
        if (sumf != sumb) flag = false;
        if (flag) cout << sub << endl;
        else cout << -1 << endl;
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读