zoukankan      html  css  js  c++  java
  • 711B

    题目大意:在num[i][j]==0处填一个数使每行,每列,对角线的和相同,若果有多种答案输出一种。

    题目思路:模拟

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stdio.h>
    #include<stdlib.h>
    #include<queue>
    #include<math.h>
    #include<map>
    #define INF 0xffffffff
    #define MAX 505
    #define Temp 1000000000
    #define MOD 1000000007
    #define LL long long
    #define PI acos(-1)
    
    using namespace std;
    
    long long num[MAX][MAX];
    
    long long change(int n,int x,int y)
    {
        long long k=0,sum1=0,sum2=0,sum3=0,sum4=0,sum=0;
        int op1=0,op2=0;
        for(int i=1; i<=n; i++)
        {
            if(i!=x)
            {
                for(int j=1; j<=n; j++)
                    k+=num[i][j];
                break;
            }
        }
        for(int i=1; i<=n; i++)
        {
            sum=0;
            if(i==x)
            {
                for(int j=1; j<=n; j++)
                    sum1+=num[i][j];
                continue;
            }
            for(int j=1; j<=n; j++)
            {
                sum+=num[i][j];
            }
            if(sum!=k)
                return -1;
        }
    
        for(int i=1; i<=n; i++)
        {
            sum=0;
            if(i==y)
            {
                for(int j=1; j<=n; j++)
                    sum2+=num[j][i];
                continue;
            }
            for(int j=1; j<=n; j++)
            {
                sum+=num[j][i];
            }
            if(sum!=k)
                return -1;
        }
        if(x==y && x==(n+1)/2 && n%2!=0)
        {
            for(int i=1; i<=n; i++)
            {
                sum3+=num[i][i];
                sum4+=num[i][n-i+1];
            }
            long long a=k-sum1;
            long long b=k-sum2;
            long long c=k-sum3;
            long long d=k-sum4;
            if(a!=b || a!=c || a!=d || b!=c || b!=d || c!=d)
                return -1;
            else if(a<1)
                return -1;
            return a;
        }
        for(int i=1; i<=n; i++)
        {
            if(num[i][i]==0)
            {
                op1=1;
            }
            sum3+=num[i][i];
            if(num[i][n+1-i]==0)
            {
                op2=1;
            }
            sum4+=num[i][n+1-i];
        }
        if(op1==0 && sum3!=k)
            return -1;
        if(op2==0 && sum4!=k)
            return -1;
        if(op1)
        {
            long long c=k-sum3;
            long long a=k-sum1;
            long long b=k-sum2;
            if(a!=b || a!=c || b!=c)
                return -1;
            else if(a < 1)
                return -1;
            return a;
        }
        if(op2)
        {
            long long c=k-sum4;
            long long a=k-sum1;
            long long b=k-sum2;
            if(a!=b || a!=c || b!=c)
                return -1;
            else if(a < 1)
                return -1;
            return a;
        }
        long long a=k-sum1;
        long long b=k-sum2;
        if(a!=b || (a==b && a<1))
            return -1;
        return a;
    }
    
    int main()
    {
        int n,x,y;
        while(scanf("%d",&n)!=EOF)
        {
            memset(num,0,sizeof(num));
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    scanf("%lld",&num[i][j]);
                    if(num[i][j]==0)
                    {
                        x=i;
                        y=j;
                    }
                }
            }
            if(n==1)
            {
                printf("2
    ");
                continue;
            }
            long long ok=change(n,x,y);
            printf("%lld
    ",ok);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Amazon EBS的功能更新
    ORA-03113: end-of-file on communication channel
    云serverlinux又一次挂载指定文件夹(非扩充)
    Binder对象死亡通知机制
    cocos2d::ui::TextField 调用setAttachWithIME和setDetachWithIME都无效
    shell linux基本命令实例、笔记
    降智严重——nowcoder练习赛46&&codeforces #561 Div2
    2018-2-13-win10-uwp-隐藏实时可视化
    2018-2-13-win10-uwp-隐藏实时可视化
    2018-2-13-win10-UWP-ListView-模仿开始菜单
  • 原文地址:https://www.cnblogs.com/alan-W/p/6042074.html
Copyright © 2011-2022 走看看