Coderforces 711B Chris and Magic Square

原创 2016年08月30日 13:19:20
模拟题,但是考虑不全面,以后还是要从整体考虑啊。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=500+10;
typedef long long LL;
LL map[maxn][maxn];
int n;
int x,y;
LL solve()
{
    if(n==1) return 1;
    LL sum=0;
    if(x==1)
        for(int i=1;i<=n;i++) sum+=map[2][i];
    else
        for(int i=1;i<=n;i++) sum+=map[1][i];
    LL aim=sum;
    for(int i=1;i<=n;i++)
        aim-=map[x][i];
    if(aim<=0) return -1;
    map[x][y]=aim;
    for(int i=1;i<=n;i++)
    {
        LL temp=0;
        for(int j=1;j<=n;j++)
        {
            temp+=map[i][j];
        }
        if(temp!=sum) return -1;
    }
    for(int j=1;j<=n;j++)
    {
        LL temp=0;
        for(int i=1;i<=n;i++)
        {
            temp+=map[i][j];
        }
        if(sum!=temp) return -1;
    }
    LL temp=0;
    for(int i=1;i<=n;i++)
        temp+=map[i][i];
    if(temp!=sum) return -1;
    temp=0;
    for(int i=1;i<=n;i++)
        temp+=map[i][n-i+1];
    if(temp!=sum) return -1;
    return aim;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>map[i][j];
                if(map[i][j]==0) {x=i;y=j;}
            }
        }
        cout<<solve()<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

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

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...

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

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

【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

模拟

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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