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

    xiaoxiao2021-03-25  97

    题目:

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

    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. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.

    Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal —  and the secondary diagonal — ) are equal.

    Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

    n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.

    It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.

    Output

    Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

    If there are multiple solutions, you may print any of them.

    Examples input 3 4 0 2 3 5 7 8 1 6 output 9 input 4 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 output 1 input 4 1 1 1 1 1 1 0 1 1 1 2 1 1 1 1 1 output -1 Note

    In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,

    The sum of numbers in each row is:

    4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.

    The sum of numbers in each column is:

    4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.

    The sum of numbers in the two diagonals is:

    4 + 5 + 6 = 2 + 5 + 8 = 15.

    In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.

    思路:

    给了一个n*n的图,让你填数,四边形的四边和两个对角线的和要加起来相等,思路很简就是他只缺一个数,只要找出其他的就能确定最后要得到的和,然后用和减去缺的这个数所在的那一条边就得到了答案,如果减下来的结果<=0那么就证明没有这个数,就输出-1

    代码1(C++):

    #include <stdio.h> #include <stdlib.h> #include <queue> #include <stack> #include <iostream> #include <cstring> #include <string> #define N 1000+20 #define inf 0x3f3f3f3f #define ll long long #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; ll map[N][N]; ll n,x1,y1,sum1,cnt,flag; void slove() { sum1=0; for(ll i=0; i<n; i++) { if(i!=x1) { for(ll j=0; j<n; j++) sum1+=map[i][j]; break; } } cnt=sum1; for(ll i=0; i<n; i++) { if(i==x1) { for(ll j=0; j<n; j++) cnt-=map[i][j]; } } map[x1][y1]=cnt; } int main() { scanf("%lld",&n); for(ll i=0; i<n; i++) for(ll j=0; j<n; j++) { scanf("%lld",&map[i][j]); if(map[i][j]==0) { x1=i; y1=j; } } if(n==1) { printf("1\n"); return 0; } slove(); if(cnt<1) { printf("-1\n"); return 0; } flag=0; for(ll i=0; i<n; i++) { ll x=0,y=0; for(ll j=0; j<n; j++) x+=map[i][j]; for(ll j=0; j<n; j++) y+=map[j][i]; if(!(x==y&&x==sum1&&y==sum1)) { printf("-1\n"); return 0; } } ll sum2=0; ll i=0,j=0; while(i<n) { sum2+=map[i][j]; i++; j++; } if(sum2!=sum1) { printf("-1\n"); return 0; } sum2=0,i=0,j=n-1; while(i<n) { sum2+=map[i][j]; i++; j--; } if(sum1!=sum2) { printf("-1\n"); return 0; } printf("%lld\n",cnt); return 0; }

    代码2(py):

    n = int(input()) k = -1 a = [] A = set() if n == 1: a.append(list(map(int, input().split()))) print(1) exit() for i in range(n): a.append(list(map(int, input().split()))) A.add(sum(a[i])) if k == -1: for j in range(n): if a[i][j] == 0: k = i l = j if len(A) > 2: print(-1) exit() elif len(A) == 2: d = max(A) - min(A) a[k][l] = d else: len(A) == 1 print(-1) exit() A.clear() s = 0 s2 = 0 for i in range(n): A.add(sum(a[i])) s += a[i][i] s2 += a[i][n-1-i] A.add(s) A.add(s2) #b = [] b = list(zip(*a)) #print(b) for i in range(n): A.add(sum(b[i])) if len(A) > 1: print(-1) else: print(d)

    转载请注明原文地址: https://ju.6miu.com/read-32864.html

    最新回复(0)