CodeForces 711B Chris and Magic Square (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等。
析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他的算出来,然后再作差就算这个数了,
然后再去验证其他的对不对就好了。除了n为1,其他的都是唯一解应该。或者没有。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#include <sstream>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e2 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL a[maxn][maxn]; int main(){
while(cin >> n){
memset(a, 0, sizeof a);
LL sum1 = 0, sum2 = 0;
int x, y;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
scanf("%I64d", &a[i][j]);
a[i][n] += a[i][j];
a[n][j] += a[i][j];
if(a[i][j] == 0) x = i, y = j;
if(i == j) sum1 += a[i][j];
if(i + j == n - 1) sum2 += a[i][j];
}
}
if(1 == n) printf("1\n");
else{
LL ans = 0;
for(int i = 0; i < n; ++i){
if(i != x){
ans = a[i][n] - a[x][n];
a[x][n] += ans;
a[n][y] += ans;
if(x == y) sum1 += ans;
if(x + y == n - 1) sum2 += ans;
break;
}
}
bool ok = true;
if(sum1 != sum2 || ans < 1 || ans > 1e18) ok = false;
if(!ok){ printf("-1\n"); continue; }
for(int i = 0; i < n; ++i){
if(a[i][n] != sum1 || a[n][i] != sum1){ ok = false; break; }
}
if(!ok){ printf("-1\n"); continue; }
else cout << ans << endl;
}
}
return 0;
}
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
简单题. 找一个不存在$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 (暴力)
Chris and Magic Square 题目链接: http://codeforces.com/contest/711/problem/B Description ZS the Coder an ...
- 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 ...
- 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 ...
随机推荐
- hihoCoder #1174 : 拓扑排序·一 (判断循环图)
G++ 261ms 13MB 题意: 给出n门课程的修读所需要的前置课程的关系,按理说应该是个拓扑图,但是因为某些原因导致了混乱,所以有可能不是一个拓扑图.现在的问题是,判断该图是否为一个拓扑图(即无 ...
- ecshop文章详情页显示浏览数
1.后台执行以下SQL语句 ) NOT NULL 2.找到article.php的这段代码 else { $smarty->display('article_pro.dwt', $cache_i ...
- ecshop首页调用评论及图片
1.在library文件夹中建立一个名为index_comment.lbi文件 2.输入以下代码 <meta http-equiv="Content-Type" conten ...
- MYSQL复制的几种模式
MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制. MYSQL复制的几种模式 MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制.简言之,这种新技 ...
- Android Studio 学习 - 程序安装
痛定思痛,从今天开始专心学习AndriodStudio.希望以此为契机,把Java学扎实.更希望自己能坚持下去,不要半途而废. 记录一些日常的工作以及碰到的问题,权当勉励自己.荀子曰:吾尝终日而思矣, ...
- [转载] ubuntu Authentication failure
ubuntu的root用户默认是禁止的,需要手动打开才行.事实上ubuntu下的所有操作都用不到root用户,由于sudo的合理使用,避免了root用户下误操作而产生的毁灭性问题.root账号启用方法 ...
- Heritrix源码分析(九) Heritrix的二次抓取以及如何让Heritrix抓取你不想抓取的URL
本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/644396 本博客已迁移到本人独立博客: http://www.yun5u ...
- 用Jquery Widgets Factory写自己的表格控件——AFGrid(支持增、删、改)
一,Jquery Widget Factory介绍 官网地址 Demo:http://jqueryui.com/widget/ API:http://api.jqueryui.com/jQuery.w ...
- Java 循环语句之多重循环
循环体中包含循环语句的结构称为多重循环.三种循环语句可以自身嵌套,也可以相互嵌套,最常见的就是二重循环.在二重循环中,外层循环每执行一次,内层循环要执行一圈. 如下所示: 例如:使用 * 打印长方形: ...
- YII Framework学习教程-YII的异常处理
异常无处不在,作为程序员,活着就是为了创造这些异常,然后修复这些异常而存在的.YII框架封装了PHP的异常,让异常处理起来更简单. 使用 YII处理错误和异常的配置方法: 你可以在入口文件中定义YII ...