codeforces711b

原创 2016年08月30日 16:18:08

一看就是个幻方,然后就是呢,给你个空位,让你填好空位,让他每一行,每一列,两个主对角线的和都相等。这个填的数必须是正整数
注意我计算每一行的和,用的是x的上一行或者下一行,但是忘记了,如果只有1个元素的话,那就gg了。(就wrong了这个点,改了就对了)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long map[510][510];
long long sum = 0;
int main()
{
    int  i , j ,n ,x , y , xn = 0, xy = 0, yn = 0 , yy = 0;
    scanf("%d",&n);
    if(n == 1){
        printf("1\n");
        return 0;
    }
    for( i = 1 ; i <= n ; i++){
        for( j = 1 ; j <= n ; j++){
            scanf("%d",&map[i][j]);
       //     getchar();
     //       if(map[i][j] == '0')
      //          printf("***\n"),x = j , y = i;
        }
    }
    for( i = 1 ; i <= n ; i++){
        for( j = 1 ; j <= n ; j++){
            if(map[i][j] == 0){
                y = i , x = j;
            }

        }
    }
  //  printf("x = %d , y = %d\n",x , y);
    if((x - 1) >= 1) xn = 1;
    if((x + 1) < n + 1) xy = 1;
    if((y - 1) >= 1) yn = 1;
    if((y + 1) < n + 1) yy = 1;
    if(yn){
        for( i = 1 ; i <= n ; i++)
           sum += map[y - 1][i];
    }
    else{
        for( i = 1 ; i <= n ; i++)
           sum += map[y + 1][i];
    }
    long long num  = sum , sum1;
    for(int i = 1 ; i <= n ; i++){
        num -= map[y][i];
    }
    map[y][x] = num;
    //printf("num = %d\n",num);
    int flag = 1;
    for( i = 1 ; i <= n ; i++){
        sum1 = 0;
        for( j = 1 ; j <= n ; j++){
            sum1 += map[i][j];
        }
       // printf("%d\n",sum1);
       if(sum1 != sum){
           printf("-1\n");
           return 0;
       }
    }
    for( i = 1 ; i <= n ; i++){
        sum1 = 0;
        for( j = 1 ; j <= n ; j++){
            sum1 += map[j][i];
        }
        if(sum1 != sum){
            printf("-1\n");
            return 0;
        }
    }
    sum1 = 0;
    for( i = 1 ; i <= n ; i++)
        sum1 += map[i][i];
    if(sum1 != sum){
       printf("-1\n");
        return 0;
    }
    sum1 = 0;
    for( i = 1 ; i <= n ; i++){
        sum1 += map[i][n + 1 - i];
    }
    if(sum1 != sum){
        printf("-1\n");
        return 0;
    }
    if(map[y][x] > 0)
    printf("%I64d\n",map[y][x]);
    else
        printf("-1\n");
    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...

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

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

模拟
  • sdfzyhx
  • sdfzyhx
  • 2016年08月30日 13:54
  • 153
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:codeforces711b
举报原因:
原因补充:

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