【模拟】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 ...
随机推荐
- (转)javaScript call 函数的用法说明
call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, [,.argN ...
- 分享:带波形的语音播放工具(wavesurfer-js)
项目名称:wavesurfer-js github地址:https://github.com/katspaugh/wavesurfer.js 官网地址:http://wavesurfer-js.org ...
- sql server 2005 大数据量插入性能对比
sql server 2005大数据量的插入操作 第一,写个存储过程,传入参数,存储过程里面是insert操作, 第二,用System.Data.SqlClient.SqlBulkCopy实例方法, ...
- 关于“SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 ”
原因:在从远程服务器复制数据到本地时出现“SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatas ...
- wpf 调用线程必须为sta 因为许多ui组件都需要
解决 办法 public void SomeMethod() { var task = System.Windows.Application.Current.Dispatcher.BeginInvok ...
- 推荐Asp.net WebApi入门教程
Web API 强势入门指南; Web API 入门指南 - 闲话安全; 实例快速上手 -ASP.NET 4.5新特性WebAPI从入门到精通; Asp.net WebApi 项目示例(增删改查).
- Lua-C交互函数
lua_gettable(lua_State * , tableIndex) //获取表的在key位置的值 过程:tableIndex为表在栈的位置,例:-2为第二个位置 , 此时会弹(出)栈作为参数 ...
- error C2220: warning treated as error - no 'object' file generated解决方法
error C2220: warning treated as error - no 'object' file generated 警讯视为错误 - 生成的对象文件 / WX告诉编译器将所有警告视为 ...
- Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- c#抽象工厂模式
抽象工厂模式向客户端提供一个接口,使得客户端在不必指定具体类型的情况下,创建多个产品族中的对象.本文采取的仍然是接着以前的那个快餐店的例子. 现在,快餐店经常良好,逐渐发展壮大,为了适合不同地方人的饮 ...