关闭

模拟-711B - Chris and Magic Square

161人阅读 评论(0) 收藏 举报
分类:

题意:给出N,下面N行N列输入。其中有一个位置是0,找出一个正整数使得行列对角线和相等


比赛时候没看到对角线的事WA的惨烈啊


#include <iostream>
#include <stdio.h>
using namespace std;
long long a[505][505];
long long sum1[505];
long long sum2[505];
int main()
{
    int n;
    cin>>n;
    if(n==1)
    {
    int t;
    cin>>t;
    printf("1\n");
    return 0;
    }
    long long flag1,flag2,flag,tep1,xx=0,gg=0;
    for(int i=1;i<=n;i++)
    {   flag=0;
        for(int j=1;j<=n;j++)
        {
            scanf("%lld",&a[i][j]);
            if(a[i][j]==0)
            {
                flag=1;
                flag1=i;
                flag2=j;
            }
            sum1[i]+=a[i][j];
        }
        if(flag==0)
            {
                if(xx==0)
                {
                    tep1=sum1[i];
                    xx=1;
                }
                if(sum1[i]!=tep1)
                    gg=1;
            }
    }
   // printf("%d %d \n",flag1,flag2);
    if(gg==1)
    {
        printf("-1\n");
        return 0;
    }
    long long tep2=0,yy=0;
    for(int j=1;j<=n;j++)
    {
        flag=0;
        for(int i=1;i<=n;i++)
        {
        if(a[i][j]==0)
        {
            flag=1;
        }
        sum2[j]+=a[i][j];
        }
        if(flag==0)
        {
            if(yy==0)
            {
                tep2=sum2[j];
                yy=1;
            }
            if(sum2[j]!=tep2)
                gg=1;
        }
    }
  //  printf("%d\n",tep2);
    if(gg==1)
    {
        printf("-1\n");
        return 0;
    }
    long long  ss=0;
    for(int j=1;j<=n;j++)
        ss+=a[flag1][j];
   // printf("ss=%d\n",tep1);
    //printf("%dgg\n",a[flag1][flag2]);
    a[flag1][flag2]=tep1-ss;
   // printf("%d\n",a[flag1][flag2]);
    if(a[flag1][flag2]<=0)
    {
        printf("-1\n");
        return 0;
    }
    if(tep1==tep2)
    {
        long long suma=0,sumb=0;
        for(int i=1;i<=n;i++)
            suma+=a[i][i];
        for(int i=1;i<=n;i++)
            sumb+=a[i][n-i+1];
//printf("%lld %lld",suma,sumb);
        if(suma==sumb&&suma==tep1)
        printf("%lld\n",a[flag1][flag2]);
        else
            printf("-1\n");
    }
    else
        printf("-1\n");
    return 0;
}


0
0
查看评论

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

codeforces#369Div2BChris and Magic Square
  • qq_17259291
  • qq_17259291
  • 2016-08-30 21:03
  • 262

【模拟】Codeforces 711B Chris and Magic Square

题目链接:   http://codeforces.com/problemset/problem/711/B 题目大意:   N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数。无解输出-1。幻方是每一行每一列和两条主对角线的和都相等。 题目思路:  ...
  • u010568270
  • u010568270
  • 2016-08-30 14:51
  • 181

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 on the entrance which is filled with integers....
  • qq_35806592
  • qq_35806592
  • 2017-03-22 20:50
  • 157

CodeForces 711B - Chris and Magic Square

//http://codeforces.com/problemset/problem/711/B //又打了一场掉分赛 这个题最后一定要判断是否小于0 不然会wa7 #include <stdio.h> #include <string> #include <cstri...
  • Katapeltes
  • Katapeltes
  • 2016-08-30 21:31
  • 169

CodeForces 711B Chris and Magic Square

http://codeforces.com/problemset/problem/711/B #include using namespace std; long long int a[555][555]; long long int b[555]; long...
  • threeh20
  • threeh20
  • 2017-10-14 19:28
  • 106

Coderforces 711B Chris and Magic Square

模拟题,但是考虑不全面,以后还是要从整体考虑啊。#include #include #include using namespace std; const int maxn=500+10; typedef long long LL; LL map[maxn][maxn]; int n; int x,...
  • qq_33901573
  • qq_33901573
  • 2016-08-30 13:19
  • 102

CodeForces - 711B Chris and Magic Square

CodeForces - 711B  Chris and Magic Square    题意:给你一个N*N矩阵,其中0代表未知的那个数,让你在这个位置填上一个数使整个矩阵的 每一行、每一列、主对角线、次对角线的和都相等。     思路:...
  • GYH0730
  • GYH0730
  • 2017-09-03 21:11
  • 71

Codeforces 711B Chris and Magic Square

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa...
  • llwwlql
  • llwwlql
  • 2016-08-30 10:16
  • 359

B. Chris and Magic Square

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output ZS ...
  • spring371327
  • spring371327
  • 2016-09-05 21:56
  • 404

C - 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 on the entrance which is filled with integers....
  • Irish_Moonshine
  • Irish_Moonshine
  • 2017-10-15 10:57
  • 91
    个人资料
    • 访问:105202次
    • 积分:7128
    • 等级:
    • 排名:第3801名
    • 原创:631篇
    • 转载:14篇
    • 译文:0篇
    • 评论:9条
    博客专栏
    openGL

    文章:15篇

    阅读:5933
    acm

    文章:493篇

    阅读:77055
    最新评论