CodeForces 711B Chris and Magic Square
简单题。
找一个不存在$0$的行,计算这行的和(记为$sum$),然后就可以知道$0$那个位置应该填的数字(记为$x$)。
如果$x<=0$,那么无解,否则再去判断每一行,每一列以及两个斜对角的和是否均为$sum$。需要注意的是$n=1$的时候,直接输出$1$就可以了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-; const int maxn=;
int n;
LL a[maxn][maxn];
int r,c; int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%lld",&a[i][j]);
if(a[i][j]==) r=i, c=j;
}
} if(n==) { printf("1\n"); } else
{
int pos=-;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
if(i!=r) { pos=i; break; }
if(pos!=-) break;
} LL sum=; for(int j=; j<=n; j++) sum=sum+a[pos][j];
LL sum2=; for(int j=; j<=n; j++) sum2=sum2+a[r][j];
a[r][c]=sum-sum2;
if(a[r][c]<=) printf("-1\n");
else
{
bool fail=;
for(int i=; i<=n; i++)
{
LL t=; for(int j=; j<=n; j++) t=t+a[i][j];
if(t!=sum) fail=;
} for(int j=; j<=n; j++)
{
LL t=; for(int i=; i<=n; i++) t=t+a[i][j];
if(t!=sum) fail=;
} LL t=;
for(int i=; i<=n; i++) t=t+a[i][i];
if(t!=sum) fail=; t=; for(int i=; i<=n; i++) t=t+a[i][n-i+];
if(t!=sum) fail=; if(fail) printf("-1\n");
else printf("%lld\n",a[r][c]);
}
}
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
题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...
- CodeForces 711B Chris and Magic Square (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了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 ...
随机推荐
- -协同IResult
Caliburn.Micro学习笔记(五)----协同IResult 今天说一下协同IResult 看一下IResult接口 /// <summary> /// Allows cust ...
- 【又长见识了】函数传参,params参数,ref和out参数详解
一.原来函数这样传参 先看一个函数和函数调用. static void Main(string[] args) { ; Test(num);//局部变量在使用之前赋值 //Test(10); //直接 ...
- 《C++游戏开发》笔记十三 平滑过渡的战争迷雾(一) 原理:Warcraft3地形拼接算法
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9611887 作者:七十一雾央 新浪微博:http:/ ...
- MongoDB学习之--增删改查(2)
昨天简单介绍了官方驱动操作MongoDB进行增删查操作的,今天继续介绍更新操作..... 方法简介 官方驱动中更新操作使用collection的Update方法,有泛型和非泛型两个版本: 其签名如下( ...
- PrintWriter返回值乱码问题
⑴response.setCharacterEncoding("utf-8"); ⑵response.setContentType("text/html; charset ...
- struts整合spring整合hibernate
1, web.xml中配置spring监听类 <listener> <listener-class>org.springframework.web.context.Contex ...
- JavaEE:Cookie和Session
Cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器.当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去.这样web资源处理的就是用户各自的数据了. ...
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...
- 一步一步深入spring(1)--搭建和测试spring的开发环境
1.引用jar包 到spring的网站上下载spring的jar包(本文是2.5.6),解压缩后找到 使用spring必须引用的jar包 spring.jar commons-logging.jar ...
- 使用ResourceDictionary管理Logical Resources
WPF整理-使用ResourceDictionary管理Logical Resources “Logical resources may be of various types, such as br ...