码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces_AIM Tech Round 3 (Div. 1)_B

时间:2016-09-10 06:39:15      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

http://codeforces.com/problemset/problem/711/B

 

比较简单,过程有点繁琐,先找一行包含那个0的行,得到和,以此填出0位置的值,然后判断这个矩阵是否符合条件。

要注意的是,n=1的情况,数据超了int,结果不为负。

 

#include<iostream>
#include<cstdio>
using namespace std;
long long a[505][505];
int main()
{
    int n,x,y;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            cin >> a[i][j];
            if(!a[i][j])
            {
                x = i;
                y = j;
            }
        }
    }
    if(n == 1)
    {
        printf("1\n");
        return 0;
    }
    long long sum = 0;
    for(int i = 1;i <= n;i++)
    {
        if(i != x)
        {
            for(int j = 1;j <= n;j++)
            {
                sum += a[i][j];
            }
            break;
        }
    }
    long long now = 0;
    for(int i = 1;i <= n;i++)
    {
        now += a[x][i];
    }
    a[x][y] = sum-now;
    for(int i = 1;i <= n;i++)
    {
        long long temp1 = 0,temp2 = 0;
        for(int j = 1;j <= n;j++)
        {
            temp1 += a[i][j];
            temp2 += a[j][i];
        }
        if(temp1 != sum || temp2 != sum)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    long long temp1 = 0,temp2 = 0;
    for(int i = 1;i <=n;i++)
    {
        temp1 += a[i][i];
        temp2 += a[i][n-i+1];
    }
    if(temp1 != sum || temp2 != sum)
    {
        cout << -1 << endl;
        return 0;
    }
    if(a[x][y] <= 0)    cout << -1 << endl;
    else                cout << a[x][y] << endl;
    return 0;
}

 

Codeforces_AIM Tech Round 3 (Div. 1)_B

标签:

原文地址:http://www.cnblogs.com/zhurb/p/5858575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!