关闭

cf#369-Chris and Magic Square-大模拟+trick

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


智障完全不会做

直接分情况讨论

在主/副对角线和都不在的情况

mdzz这题太坑,最后答案要是正数,

需要特判ans<=0,输出-1

否则GGGGG 又挂了



#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
typedef long long  ll;

ll mp[505][505];
ll sumx[505];
ll sumy[505];
ll dui1,dui2;
int  main()
{
    ll n;
    cin>>n;
    ll val;
    ll mx,my;
    for (ll i=1; i<=n; i++)
    {
        for (ll j=1; j<=n; j++)
        {
            scanf("%lld",&mp[i][j]);
            if (mp[i][j]==0)
            {
                mx=i,my=j;
            }
        }
    }
    if (n==1)
    {
        printf("1\n");
        return 0;
    }
    for (ll i=1; i<=n; i++)
    {
        for (ll j=1; j<=n; j++)
        {
            sumx[i]+=mp[i][j];
            sumy[j]+=mp[i][j];
            if (i==j) dui1+=mp[i][j];
            if (i+j==n+1) dui2+=mp[i][j];
        }
    }

    if (mx!=my &&  mx+my!=n+1 )
    {
        ll flag=0,pos;
        for (ll i=1; i<=n; i++)
        {
            if (sumx[i]!=dui1) flag++,pos=i;
        }
        if (flag!=1)
        {
            printf("-1\n");
            return 0;
        }
          val=dui1-sumx[mx];
        sumy[my]+=val;
        flag=1;
        for (ll i=1; i<=n; i++)
        {
            if (sumy[i]!=dui1) flag=0;
        }
        if (dui2!=dui1) flag=0;
        if (flag);
         else val=-1;
            //printf("-1\n");
    }
    else  if ((n%2&& mx==my&&mx!=n/2+1)||(n%2==0 && mx==my))
    {
        ll aa;
        for (aa=1; aa<=n; aa++)
            if (aa!=mx) break;
          val=sumx[aa]-dui1;
        sumx[mx]+=val;
        sumy[my]+=val;
        dui1+=val;
        ll  flag=1;
        for (ll i=1; i<=n; i++)
        {
            if (sumy[i]!=sumx[aa]) flag=0;
        }
        for (ll i=1; i<=n; i++)
        {
            if (sumx[i]!=sumx[aa]) flag=0;
        }
        if (dui2!=sumx[aa]) flag=0;
         if (flag);
         else val=-1;
    }
    else if (mx+my==n+1&& !(mx==n/2+1&&my==n/2+1))
    {

        ll aa;
        for (aa=1; aa<=n; aa++)
            if (aa!=mx) break;
          val=sumx[aa]-dui2;
        sumx[mx]+=val;
        sumy[my]+=val;
        dui2+=val;
        ll flag=1;
        for (ll i=1; i<=n; i++)
        {
            if (sumy[i]!=sumx[aa]) flag=0;
        }
        for (ll i=1; i<=n; i++)
        {
            if (sumx[i]!=sumx[aa]) flag=0;
        }
        if (dui1!=sumx[aa]) flag=0;
         if (flag);
         else val=-1;
    }
    else
    {

        ll flag=1;
        for (ll i=1; i<=n/2; i++)
            if (sumx[i]!=sumx[1]) flag=0;
        for (ll i=n/2+2; i<=n; i++)
            if (sumx[i]!=sumx[1]) flag=0;
        for (ll i=1; i<=n/2; i++)
            if (sumy[i]!=sumx[1]) flag=0;
        for (ll i=n/2+2; i<=n; i++)
            if (sumy[i]!=sumx[1]) flag=0;
          val=sumx[1]-dui1;
          val=sumx[1]-dui1;
        dui1+=val;
        dui2+=val;
        if (dui2!=sumx[1]) flag=0;
        if (flag);
         else val=-1;
    }
    if (val<=0)
        val=-1;
    printf("%lld\n",val);

    return 0;

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:202855次
    • 积分:8698
    • 等级:
    • 排名:第2275名
    • 原创:735篇
    • 转载:4篇
    • 译文:0篇
    • 评论:26条
    233
    文章分类
    最新评论