【题目链接】: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. Bootloader - main system - Recovery的三角关系【转】

    本文转载自:http://blog.csdn.net/u012719256/article/details/52304273 一.MTD分区: BOOT:        boot.img,Linux ...

  2. java replaceAll 忽略大小写

    public static void main(String[] args) { String temp=" CLASS_path : /aaabb/"; System.out.p ...

  3. oracle 误删数据

    insert into hr.job_history select * from hr.job_history as of timestamp to_timestamp('2007-07-23 10: ...

  4. linux top 的用法

    本篇博文主要讲解有关top命令,top命令的主要功能是查看进程活动状态以及一些系统状况. TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终 ...

  5. Linux命令(005) -- kill、pkill和killall的比较

    kill命令用来“杀掉”指定进程PID的进程.终止一个前台进程可以使用Ctrl+C,终止一个后台进程就须用kill命令.kill命令是通过向进程发送指定的信号来结束相应进程的.在默认情况下,kill命 ...

  6. Nginx一个实现负载均衡的强大web server

    <转>nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 〉直接作为http server(代替apache,对PHP需要FastCGI处理器支持): 〉另外一个 ...

  7. Spring的核心机制依赖注入

    原文地址:http://developer.51cto.com/art/200610/33311.htm 本文主要讲解依赖注入(设值注入.构造注入),作用是可以使Spring将各层的对象以松耦合的方式 ...

  8. html5——背景

    背景大小 background-size: % %;//宽 高 background-size: 100px 100px;//宽 高 background-size: cover;//覆盖,图片会完全 ...

  9. html5——多媒体(一)

    <audio> 1.autoplay 自动播放 2.controls 是否显不默认播放控件 3.loop 循环播放 4.preload 预加载 同时设置autoplay时些属性失效 5.由 ...

  10. 如何设置“用eclipse开发时自动在顶端产生import”?

    eclipse新版本中的默认设置可能导致自动在生成import时是在代码中,而大多数时间我们需要自动生成import在代码顶端,按照如下设置即可纠正