codeforces 711B B. Chris and Magic Square(水题)
题目链接:
题意:
问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=4e5+10;
const int maxn=1e3+520;
const double eps=1e-12; LL mp[505][505],l[505],r[505];
int main()
{
int n,x=0,y=0;
read(n);
For(i,1,n)
{
r[i]=0;
For(j,1,n)
{
read(mp[i][j]);
if(mp[i][j]==0)
{
x=i;y=j;
}
r[i]+=mp[i][j];
}
}
if(n==1)
{
cout<<"1\n";
return 0;
}
int num=0;
for(int i=2;i<=n;i++)
{
if(r[i]!=r[i-1])num++;
}
if(num>2)cout<<"-1\n";
else
{
LL temp;
if(x>1)temp=r[1];
else temp=r[2];
LL sum=temp;
for(int i=1;i<=n;i++)
{
if(mp[x][i])temp-=mp[x][i];
}
mp[x][y]=temp;
if(temp<=0)cout<<"-1\n";
else
{
for(int i=1;i<=n;i++)
{
l[i]=0;
for(int j=1;j<=n;j++)
{
l[i]+=mp[j][i];
}
}
for(int i=2;i<=n;i++)
{
if(l[i]!=sum)
{
cout<<"-1\n";
return 0;
}
}
temp=0;
for(int i=1;i<=n;i++)
{
temp+=mp[i][i];
}
if(temp!=sum)
{
cout<<"-1\n";
return 0;
}
temp=0;
for(int i=1;i<=n;i++)
{
temp+=mp[i][n-i+1];
}
if(temp!=sum)
{
cout<<"-1\n";
return 0;
}
cout<<mp[x][y]<<endl;
}
} return 0;
}
codeforces 711B B. Chris and Magic Square(水题)的更多相关文章
- 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 ...
- 【codeforces 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- Xtreme8.0 - Magic Square 水题
Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...
- codeforces #369div2 B. Chris and Magic Square
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...
- 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 ...
- Chris and Magic Square CodeForces - 711B
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...
- B. Chris and Magic Square
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces Gym 100187L L. Ministry of Truth 水题
L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- Codeforces Round #185 (Div. 2) B. Archer 水题
B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...
随机推荐
- java微信开发(wechat4j)——支持微信JS-SDK的jsapi_ticket中控服务器
jsapi_ticket是使用js-sdk必须要的一个凭证,需要配置在js中. jsapi_ticket获取 要获取jsapi_ticket可以使用如下的方法 String jsapi_ticket ...
- JY游戏之手游《打卡乐猫》
JY游戏之手游戏<打卡乐猫> JY是一款专门制作js小游戏的js库,它集成了一些对网页类小游戏的功能帮助 ,能帮你更快的完成js开发,它的主体框架包JY.JYG.Sprite.Sprite ...
- 挖掘机技术哪家强(c++实现)
描述:为了用事实说明挖掘机技术到底哪家强,组织一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入:输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位参赛者的 ...
- C#将DataTable转化为List<T>
C#将DataTable转化为List<T> 在使用三层架构开发一个网站时,希望把DataTable对象转换为List<T>对象,于是在网上找资料,总结一个比较方便的方法来实现 ...
- SET UPDATE TASK LOCAL
SET Effect Switches on the local update task. This means that when you specify CALL FUNCTION ... IN ...
- Engine中如何更改lyr文件数据源的相对路径
以下是使用relativebase存储layerfile的代码,如果需要修改数据源只要修改ILayer即可: string folderpath = @E:相对路径; string layerfile ...
- appstore 提交警告 - Missing iOS Distribution signing identity for xxxx
提交app至appstore的时候出现如下错误: 注:本解决方案仅适用于Keychain中AppleWWDRCA.cer过期问题,表现为Keychain中的各种开发者证书失效,失效原因均为证书的颁发机 ...
- 不小心改了Xcode系统的头文件,运行报错,解决办法
- UITableView小知识点总结
1.UITableView去除空的cell,多余不用的 在viewdidload方法里加上这一句即可 self.tableView.tableFooterView = [[UIView alloc] ...
- Reveal使用步骤和 破解Revealapp的试用时间限制
下载地址:http://pan.baidu.com/s/1eQstR2M 一.Reveal使用步骤 1.启动Reveal --> Help --> Show Reveal Library ...