【模拟】Codeforces 711B Chris and Magic Square
题目链接:
http://codeforces.com/problemset/problem/711/B
题目大意:
N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数。无解输出-1。幻方是每一行每一列和两条主对角线的和都相等。
题目思路:
【模拟】
题目没看清外加爆intWA了好多次。。罪过。
求出每一行每一列和对角线的和,为0的那个用随意一行的和扣去0的那一行就可以得到。只要验证其余的是否都相等即可。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 504
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
LL a[N][N];
LL sum[N],tot[N];
int sx,sy;
bool judge()
{
int i,j;
LL ll,c;
if(sx==)
a[sx][sy]=sum[]-sum[sx];
else a[sx][sy]=sum[]-sum[sx];
sum[sx]+=a[sx][sy],tot[sy]+=a[sx][sy];
c=sum[];
for(i=;i<=n;i++)
if(sum[i]!=c)
return ;
for(j=;j<=n;j++)
if(tot[j]!=c)
return ;
for(i=,ll=;i<=n;i++)
ll+=a[i][i];
if(ll!=c)return ;
for(i=,ll=;i<=n;i++)
ll+=a[i][n+-i];
if(ll!=c)return ;
return ;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k; // for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
mem(sum,);mem(tot,);
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
scanf("%I64d",&a[i][j]);
if(a[i][j]==)sx=i,sy=j;
sum[i]+=a[i][j];
tot[j]+=a[i][j];
}
}
if(n==)
{
puts("");
continue;
}
if(judge() && a[sx][sy]>)printf("%I64d\n",a[sx][sy]);
else puts("-1");
}
return ;
}
/*
// //
*/
【模拟】Codeforces 711B Chris and Magic Square的更多相关文章
- codeforces 711B - Chris and Magic Square(矩阵0位置填数)
题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...
- CodeForces 711B Chris and Magic Square (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
- CodeForces 711B Chris and Magic Square
简单题. 找一个不存在$0$的行,计算这行的和(记为$sum$),然后就可以知道$0$那个位置应该填的数字(记为$x$). 如果$x<=0$,那么无解,否则再去判断每一行,每一列以及两个斜对角的 ...
- 711B - Chris and Magic Square 模拟
题目大意:在num[i][j]==0处填一个数使每行,每列,对角线的和相同,若果有多种答案输出一种. 题目思路:模拟 #include<iostream> #include<algo ...
- CF 711B - Chris and Magic Square
挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include ...
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- 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 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 ...
随机推荐
- Mysql表复制及备份还原
1.复制表结构 1.1 含有主键等信息的完整表结构 CREATE table 新表名 LIKE book; 1.2 只有表结构,没有主键等信息 create table 新表名 s ...
- 自己动手写控件(模仿mvc htmlhelper的类)
自定义helper类,要求命名空间在 System.Web.Mvc之下,要求,静态类,静态方法,特殊生成对应html的返回字段, 传递Htmlhleper,返回特定类型 返回值是MvcHtmlStri ...
- 把Excel数据导入到数据库
引入命名空间 using System.IO; using System.Data; using System.Data.OleDb; 引入命名空间 首先要把Excel上传到服务器 //上传Excel ...
- js中浮点型运算 注意点
先看张图: 这是一个JS浮点数运算Bug,导致我树状图,数据合计不正确,,,,,,两个小数相加,出来那么多位小数 (这是修该之后的) 网上找到以下解决方式: 方法一:有js自定义函数 <sc ...
- Missing artifact com.sun:tools:jar:1.5.0
http://java-suddy.iteye.com/blog/1821871(解决了我的问题)
- vpn的作用
1.可以用于远程对方桌面. 步骤: 1.浏览器中访问网址,输入用户名,密码即可 2.输入远程桌面用户名和网址
- zoj3839-Poker Face
#include<cstdio>int n;void P(int i,int j,int n,int f){ if(i==n){ for(int k=1;k<=n;k++)print ...
- javascript 函数声明问题
(function(){ //运行正常 test1(); function test1() { console.log('123'); }; })() (function(){ //出错,test2未 ...
- Windows手动搭建PHP运行环境
首先~可以先在目录里面创建一个wamp目录,我的创建在 E: 盘 1.0 下载Apache2.4,x64位.VC11组件[电脑多少位装多少位] apache下载地址:https://www.apach ...
- DOS命令行使用pscp实现远程文件和文件夹传输(转)
转自 http://snailwarrior.blog.51cto.com/680306/141201 pscp是putty安装包所带的远程文件传输工具,使用和Linux下scp命令相似,具体的使 ...