CodeForces 711B - Chris and Magic Square

原创 2016年08月30日 21:31:34
//http://codeforces.com/problemset/problem/711/B
//又打了一场掉分赛 这个题最后一定要判断是否小于0 不然会wa7


#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <functional>
#include <vector>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
int main()
{
    cin.sync_with_stdio(false);
    long long A[505][505]= {0};
    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]==0)
                X=i,Y=j;
        }
    if (N==1)
    {
        cout<<1;
        return 0;
    }
    long long sum=0;
    bool ok=true;
    for (int i=1; i<=N; i++)
    {
        if (i==X)
            continue;
        for (int j=1; j<=N; j++)
            sum+=A[i][j];
        break;
    }
    for (long long i=1,x=0; i<=N; i++,x=0)
    {
        if (i==X)
            continue;
        for (int j=1; j<=N; j++)
            x+=A[i][j];
        if (x!=sum)
            ok=false;
    }
    for (long long j=1,x=0; j<=N; j++,x=0)
    {
        if (j==Y)
            continue;
        for (int i=1; i<=N; i++)
            x+=A[i][j];
        if (x!=sum)
            ok=false;
    }
    long long x=0;
    if (!ok)
        cout<<-1;
    else
    {
        x=0;
        for (int j=1; j<=N; j++)
            x+=A[X][j];
        A[X][Y]=sum-x;
        x=0;
        for (int i=1; i<=N; i++)
            x+=A[i][Y];
        if (x!=sum)
            ok=false;
        x=0;
        for (int i=1; i<=N; i++)
            x+=A[i][i];
        if (x!=sum)
            ok=false;
        x=0;
        for (int i=1; i<=N; i++)
            x+=A[i][N+1-i];
        if (x!=sum)
            ok=false;
        if (!ok)
            cout<<-1;
        else
        {
            if (A[X][Y]<=0)
                cout<<-1;
            else
                cout<<A[X][Y];
        }
    }
    return 0;
}
版权声明:转载请注明出处

CodeForces 711B Chris and Magic Square(找规律)

题目: B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabyte...

codeforces 711B. Chris and Magic Square【模拟】

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...
  • Bcwan_
  • Bcwan_
  • 2016年08月29日 23:32
  • 603

Chris and Magic Square CodeForces - 711B

ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o...

Codeforces 711B- Chris and Magic Square

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...
  • LDUtyk
  • LDUtyk
  • 2016年08月31日 18:44
  • 432

【codeforces711B】Chris and Magic Square

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...

Codeforces Problem 711B Chris and Magic Square(constructive algorithms)

Codeforces Problem 711B Chris and Magic Square(constructive algorithms)

codeforces#369Div2(711B)Chris and Magic Square

codeforces#369Div2BChris and Magic Square

【codeforces 711B】Chris and Magic Square

【题目链接】:http://codeforces.com/contest/711/problem/B【题意】 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行...

codeforces711B Chris and Magic Square 简单枚举

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...

【Codeforces Round #369 (Div. 2)】Codeforces 711B Chris and Magic Square

模拟
  • sdfzyhx
  • sdfzyhx
  • 2016年08月30日 13:54
  • 154
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces 711B - Chris and Magic Square
举报原因:
原因补充:

(最多只允许输入30个字)