Codeforces #369(Div.2)B.Chris and Magic Square(矩阵找数)
来源:互联网 发布:犀牛软件 视角 加速器 编辑:程序博客网 时间:2024/05/09 10:09
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?
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 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.
34 0 23 5 78 1 6
9
41 1 1 11 1 0 11 1 1 11 1 1 1
1
41 1 1 11 1 0 11 1 2 11 1 1 1
-1
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-1e18)的数,使得每行每列和主对角线上的和相同,如果找不到方案,输出-1.
先将空位记录下来,然后行列比较时将空位所在行列,对角线是否相同,若相同用但行总数减去空位所在行总数,即为要填的数,否则输出-1;
#include<stdio.h>#include<string.h>using namespace std;#define ll __int64ll a[505][505];int main(){ int n; while(~scanf("%d",&n)) { int row; int col; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { scanf("%I64d",&a[i][j]); if(a[i][j]==0) { row=i; col=j;//记录零的位置 } } } if(n==1) { printf("1\n"); continue; }//特殊情况考虑 int flag=0; ll rowsum=-1; for(int i=1; i<=n; i++)//每行相加 { if(i==row)continue; ll tmp=0; for(int j=1; j<=n; j++) { tmp+=a[i][j]; } if(rowsum==-1)rowsum=tmp; else { if(rowsum!=tmp)flag=1; } } if(flag==1) { printf("-1\n"); continue; } for(int i=1; i<=n; i++)//每列相加 { if(i==col)continue; ll tmp=0; for(int j=1; j<=n; j++) { tmp+=a[j][i]; } if(tmp!=rowsum)flag=1; } if(flag==1) { printf("-1\n"); continue; } ll tmp=0; for(int i=1; i<=n; i++)//对角线比较 { tmp+=a[row][i]; } if(rowsum-tmp>=1) a[row][col]=rowsum-tmp; else { printf("-1\n"); continue; } ll tmpp=0; for(int i=1;i<=n;i++) { tmpp+=a[i][i]; } if(tmpp!=rowsum) { printf("-1\n"); continue; } tmpp=0; for(int i=n; i>=1; i--) { tmpp+=a[i][n-i+1]; } if(tmpp!=rowsum) { printf("-1\n"); continue; } printf("%I64d\n",a[row][col]); }}
- Codeforces #369(Div.2)B.Chris and Magic Square(矩阵找数)
- CodeForces 711B Chris and Magic Square(找规律)
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square
- Codeforces Round #369 (Div. 2)B. Chris and Magic Square
- Codeforces #369(Div.2)B.Chris and Magic Square【思维】
- 【Codeforces Round #369 (Div. 2)】Codeforces 711B Chris and Magic Square
- CodeForces 711B. Chris and Magic Square(水题)
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square 数学、幻方
- Codeforces 711B Chris and Magic Square
- Codeforces 711B Chris and Magic Square
- CodeForces 711B - Chris and Magic Square
- Codeforces 711B- Chris and Magic Square
- codeforces B. Chris and Magic Square
- Codeforces 711 B Chris and Magic Square
- codeforces#369Div2(711B)Chris and Magic Square
- CF-B. Chris and Magic Square(细节把握)
- [Codeforces - Gym100801H (NEERC)] Hash Code Hacker (字符串构造)
- web.xml
- String.split()应该怎么用?
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 link cut tree
- lvs+keepalived实现双机热备
- Codeforces #369(Div.2)B.Chris and Magic Square(矩阵找数)
- matlab的常量和变量&&运算符
- Java实现mysql增删改查--我的JDBC学习
- 第二周项目3-1 体验复杂度 两种算法的排序时间
- C++——算法基础之排序——冒泡排序(优化)
- STM32笔记:RCC结构与配置流程
- 资源分配图化简法
- POJ EXTENDED LIGHTS OUT(开关问题模版题)
- Android四大组件