浙江财经大学
信息管理与工程学院

Codeforces Round #369 (Div. 2) B – Chris and Magic Square

本文由 Ocrosoft 于 2016-08-30 18:24:46 发表

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, jn) 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.

Solution

题意:一个矩阵中有一个位置是0,需要在这里填上一个数字,使:每一行、每一列、两对角线的和都相等。
思路:(都不知道为什么,被hack了…重新写了一个。)
可以选择非0所在的任意行(或列),计算这一行的和s1,再计算0所在的那一行的和s2,s1-s2就是要填的数。
但是要注意,当n为1时,填最小的数,1;当是s1-s2小于等于0的时候,因为这个位置只能填正数,所以是无解。
最后检查一下,行、列、对角线和是否全相等即可。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define ms(a) memset(a,0,sizeof(a))
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 500 + 10;
const int MOD = 100000;
using namespace std;
int n;
LL mp[MAXN][MAXN];
bool check(LL s1)
{
	bool flag = 0;
	LL tmp = 0;
	for (int i = 1; i <= n; i++, tmp = 0)
	{
		for (int j = 1; j <= n; j++)tmp += mp[i][j];
		if (tmp != s1) flag = 1;
	}
	if (flag)return flag;
	tmp = 0;
	for (int j = 1; j <= n; j++, tmp = 0)
	{
		for (int i = 1; i <= n; i++)tmp += mp[i][j];
		if (tmp != s1) flag = 1;
	}
	if (flag)return flag;
	tmp = 0;
	for (int i = 1; i <= n; i++) tmp += mp[i][i];
	if (tmp != s1) flag = 1;
	if (flag)return flag;
	tmp = 0;
	for (int i = 1; i <= n; i++)tmp += mp[i][n - i + 1];
	if (tmp != s1) flag = 1;
	return flag;
}
int main()
{
	cin >> n;
	int sx, sy;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			scanf("%d", &mp[i][j]);
			if (mp[i][j] == 0)sx = i, sy = j;
		}
	}
	if (n == 1)printf("1\n");
	else
	{
		int pos = sx==1 ? 2 : 1;
		LL s1 = 0, s2 = 0;

		for (int i = 1; i <= n; i++)
		{
			s1 += mp[pos][i];
			s2 += mp[sx][i];
		}

		mp[sx][sy] = s1 - s2;

		if (mp[sx][sy] <= 0) printf("-1\n");
		else
		{
			if (check(s1)) printf("-1\n");
			else printf("%I64d\n", mp[sx][sy]);
		}
	}
	return 0;
}

欢迎分享与转载,请保留链接与出处。Ocrosoft » Codeforces Round #369 (Div. 2) B – Chris and Magic Square

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址