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 ...
随机推荐
- U-boot的环境变量值得注意的有两个: bootcmd 和bootargs
本文转载至:http://www.cnblogs.com/cornflower/archive/2010/03/27/1698279.html U-boot的环境变量值得注意的有两个: bootcmd ...
- 在Android设备上判断设备是否支持摄像头
private boolean hasCamera(){ boolean hasCamera=false; PackageManager pm=getActivity().getPackageMana ...
- java jdk environment variables
1. create system variable 2. edit the system path note: ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 3. cre ...
- PHP学习笔记:keditor的使用
keditor时一个免费的开源编辑器,很多公司在使用(百度编辑器也不错).最近为了做一个客户信息管理系统,在发送邮件模块用到这个编辑器,也算学习一下新的东西. 第一步:下载编辑器 到它的官网下载:ht ...
- PHP学习笔记:对命名空间(namespace)学习资料的翻译
Name collisions means: you create a function named db_connect, and somebody elses code that you use ...
- 开源项目Foq简介
Foq是一个轻量级-线程安全的mocking类库.使用它来mock抽象类与接口这是我们通常的做法.Foq的名字来自Moq,如果你使用过Moq的话,自然后联想到它能做什么.Foq主要是为了F#的 ...
- 每日微软面试题——day 6(打印所有对称子串)
每日微软面试题——day 6(打印所有对称子串) 分类: 2.数据结构与算法2011-08-14 14:27 9595人阅读 评论(15) 收藏 举报 面试微软string测试systemdistan ...
- Android自动更新安装后显示‘完成’‘打开’按钮
/** * 安装apk * * @param url */ private void installApk() { File apkfile = new File(apkFilePath); if ( ...
- 弃用的异步get和post方法之代理方法
#import "ViewController.h" #import "Header.h" @interface ViewController () <N ...
- java调用python代码
同样的我们需要安装jython,具体的步骤如下: 1. 去 http://sourceforge.net/projects/jython/ 下载最新的jython相关的jar包. 2. 下载下来的ja ...