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所在的位置填一個(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末杯缺,一起剝皮案震驚了整個(gè)濱河市掏愁,隨后出現(xiàn)的幾起案子衫画,更是在濱河造成了極大的恐慌毫炉,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件削罩,死亡現(xiàn)場(chǎng)離奇詭異瞄勾,居然都是意外死亡费奸,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)进陡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)愿阐,“玉大人,你說(shuō)我怎么就攤上這事趾疚∮Ю” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵糙麦,是天一觀的道長(zhǎng)辛孵。 經(jīng)常有香客問(wèn)我,道長(zhǎng)赡磅,這世上最難降的妖魔是什么魄缚? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮焚廊,結(jié)果婚禮上冶匹,老公的妹妹穿的比我還像新娘。我一直安慰自己咆瘟,他們只是感情好嚼隘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著搞疗,像睡著了一般嗓蘑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上匿乃,一...
    開(kāi)封第一講書(shū)人閱讀 51,554評(píng)論 1 305
  • 那天桩皿,我揣著相機(jī)與錄音,去河邊找鬼幢炸。 笑死泄隔,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的宛徊。 我是一名探鬼主播佛嬉,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼闸天!你這毒婦竟也來(lái)了暖呕?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤苞氮,失蹤者是張志新(化名)和其女友劉穎湾揽,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡库物,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年霸旗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片戚揭。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡诱告,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出民晒,到底是詐尸還是另有隱情精居,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布镀虐,位于F島的核電站箱蟆,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏刮便。R本人自食惡果不足惜空猜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望恨旱。 院中可真熱鬧辈毯,春花似錦、人聲如沸搜贤。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)仪芒。三九已至唁影,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間掂名,已是汗流浹背据沈。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留饺蔑,地道東北人锌介。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像猾警,于是被迫代替她去往敵國(guó)和親孔祸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

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