CodeForces 711B Chris and Magic Square

題目:

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所在的位置填一個數(shù),使得最后的矩陣每行,每列以及兩條對角線的和都相等(如果沒有就輸出-1)。
這道題根據(jù)其他非0行求出0這個位置的值,然后遍歷每行每列和兩條對角線即可,但要注意一些細(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時的特殊情況;
    //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的位置不在兩條對角線上且對角線上的和已經(jīng)相等或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;//沒有補(bǔ)數(shù)就已經(jīng)相等;
            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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,563評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,694評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,672評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,965評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,690評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,019評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,013評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,188評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,718評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,438評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,667評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,149評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,845評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,252評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,590評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,384評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內(nèi)容