【题目链接】:http://codeforces.com/contest/711/problem/B

【题意】



让你在矩阵中一个空白的地方填上一个正数;

使得这个矩阵两个对角线上的和;

每一行的和,每一列的和都相同;

【题解】



对于n=1的情况,任意输出一个数字就好;

对于n>1的情况;

先算出不包括空白格子的行的所有元素的和->he;

然后对于其他的可行的行和列,算出它们的和;

一旦与he不一样,直接输出-1无解;

然后包括空白格子的行和列,它们除了那个空白格子的和也要相同->设为sphe;

然后是对角线;

如果包括了空白格子,就先算出和;看看和sphe是不是一样;

如果不包括空白格子,算出的和直接与he比较;

两个对角线同理;

最后

sphe必须要小于he

即严格<不能相同

因为要为正数



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 510; LL a[N][N],hang[N],lie[N],he=-1;
int n,sx,sy; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n);
if (n==1)
return pri(1<<endl),0;
rep1(i,1,n)
rep1(j,1,n)
{
rei(a[i][j]);
if (!a[i][j])
sx = i,sy = j;
}
rep1(i,1,n)
{
LL temp = 0;
rep1(j,1,n)
temp+=a[i][j];
hang[i] = temp;
if (sx==i) continue;
if (he==-1)
he = temp;
else
if (he!=temp)
return pri(-1<<endl),0;
}
rep1(j,1,n)
{
LL temp = 0;
rep1(i,1,n)
temp+=a[i][j];
lie[j] = temp;
if (sy==j) continue;
if (he!=temp)
return pri(-1<<endl),0;
}
bool in1 = false;
LL he1 = 0;
rep1(i,1,n)
{
if (i==sx && i==sy) in1 = true;
he1+=a[i][i];
}
if (!in1 && he1!=he) return pri(-1<<endl),0;
if (in1 && he1!=hang[sx]) return pri(-1<<endl),0;
bool in2 = false;
LL he2 = 0;
rep1(i,1,n)
{
if (i==sx && n-i+1==sy) in2 = true;
he2+=a[i][n-i+1];
}
if (!in2 && he2!=he) return pri(-1<<endl),0;
if (in2 && he2!=hang[sx]) return pri(-1<<endl),0;
if (hang[sx]<he)
{
pri(he-hang[sx]);
}
else
pri(-1<<endl);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 711B】Chris and Magic Square的更多相关文章

  1. codeforces 711B B. Chris and Magic Square(水题)

    题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...

  2. 【codeforces 255D】Mr. Bender and Square

    [题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...

  3. 【23.15%】【codeforces 703C】Chris and Road

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. codeforces #369div2 B. Chris and Magic Square

    题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...

  5. Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题

    B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...

  6. Codeforces Round #369 (Div. 2) B. Chris and Magic Square (暴力)

    Chris and Magic Square 题目链接: http://codeforces.com/contest/711/problem/B Description ZS the Coder an ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【硅谷问道】Chris Lattner 访谈录(下)

    [硅谷问道]Chris Lattner 访谈录(下) Chris Lattner 访谈录(下) 话题 Swift 在 Server 和操作系统方面有着怎样的雄心抱负? Swift 与 Objectiv ...

  9. 【硅谷问道】Chris Lattner 访谈录(上)

    [硅谷问道]Chris Lattner 访谈录(上) 话题 Chris Lattner 是谁? Xcode 的编译器 LLVM 背后有怎样的故事? Swift 诞生的前世今生,封闭的苹果为何要拥抱开源 ...

随机推荐

  1. C#的内存管理知识 .

    本章介绍内存管理和内存访问的各个方面.尽管运行库负责为程序员处理大部分内存管理工作,但程序员仍必须理解内存管理的工作原理,了解如何处理未托管的资源. 如果很好地理解了内存管理和C#提供的指针功能,也就 ...

  2. 【Poj1325】Machine Schedule机器调度

    目录 List Description Input Output Sample Input Sample Output HINT Solution Code Position: http://poj. ...

  3. [BZOJ 1691] 挑剔的美食家

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1691 [算法] 不难想到如下算法 : 将所有牛和牧草按鲜嫩程度降序排序,按顺序扫描, ...

  4. element-ui table 页面加载时,动态渲染后台传过来的数据(springmvc)

    jsp页面 <%@ page contentType="text/html;charset=UTF-8" language="java" %> &l ...

  5. zookeeper单机安装

    安装zookeeper步骤: 1,下载zookeeper http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/ 2,放到合适目录,解压 ...

  6. [jzoj NOIP2018模拟10.23]

    丢分主要是下面几个方面: 1.T2代码交错了,有个特判没写丢了10分 2.T1线段树加等差数列写错了(其实二维差分就可以,但我当时不会) 3.T3思考再三还是为了10分写上了主席树,还是写错了 总体评 ...

  7. H5 触摸事件

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  8. NOIP真题汇总

    想想在NOIP前总得做做真题吧,于是长达一个月的刷题开始了 涉及2008-2016年大部分题目 NOIP [2008] 4/4 1.传纸条:清真的三维DP 2.笨小猴:字符串模拟 3.火柴棒等式:打表 ...

  9. ACM_小游戏(棋盘博弈)

    Problem Description: 最近kiki无事可做,于是他想玩棋盘游戏.棋盘的大小是n * m.首先,棋子放置在右上角(1,m). 每次可以将棋子向左方,下方或左下方移动一个位置.当移动到 ...

  10. SRM-697-DIV2

    Div2 Medium: DivisibleSetDiv2 Problem Statement      You are given a vector <int> b containing ...