题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等

题目链接:http://codeforces.com/contest/711/problem/B

分析:题目不难,找到要填的那个数填进去,然后循环比较每行每列以及对角线的和是否相等,题目提交上去卡了几次要注意几点

注意:1.答案数据范围$1\leq x \leq 1e18$,要用 long long

2.特殊情况,$n \equiv 1$时,由于一定有要填的数,所以一定有解

3.反正就是要注意看清楚题目和数据边界情况处理啦

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long mp[][];
long long row[],col[];
int main()
{
int n;
scanf("%d",&n);
memset(row,,sizeof(row));
memset(col,,sizeof(col));
int x,y;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==)
{
x=i;y=j;
}
row[i]+=mp[i][j];
col[j]+=mp[i][j];
}
}
bool is=;
if(x==)
{
mp[x][y]=row[]-row[];
row[x]+=mp[x][y];
col[y]+=mp[x][y];
if(col[]!=row[])
is=;
else
{
for(int i=;i<n;i++)
{
if(row[i]!=row[])
{
is=;break;
}
if(col[i]!=col[])
{
is=;break;
}
}
}
}
else
{
mp[x][y]=row[]-row[x];
row[x]+=mp[x][y];
col[y]+=mp[x][y];
if(col[]!=row[])
is=;
else
{
for(int i=;i<n;i++)
{
if(row[i]!=row[])
{
is=;break;
}
if(col[i]!=col[])
{
is=;break;
}
}
}
}
long long dia1=,dia2=;
for(int i=,j=n-;(i<n&&j>=);i++,j--)
{
dia1+=mp[i][i];
dia2+=mp[i][j];
}
if(dia1!=row[])
is=;
else if(dia2!=dia1)
is=;
if(n==) //注意特判
cout<<""<<endl;
else if(is&&mp[x][y]>=) //注意答案要>=1
cout<<mp[x][y]<<endl;
else
cout<<"-1"<<endl;
return ;
}

codeforces #369div2 B. Chris and Magic Square的更多相关文章

  1. codeforces 711B B. Chris and Magic Square(水题)

    题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...

  2. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. B. Chris and Magic Square

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. codeforces 711B - Chris and Magic Square(矩阵0位置填数)

    题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...

  8. 【模拟】Codeforces 711B Chris and Magic Square

    题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...

  9. CodeForces 711B Chris and Magic Square (暴力,水题)

    题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...

随机推荐

  1. robots.txt文件没错,为何总提示封禁

    大家好,我的robots.txt文件没错,为何百度总提示封禁,哪位高人帮我看看原因,在此谢过. 我的站点www.haokda.com,robots.txt如下: ## robots.txt for P ...

  2. java获取文件的md5值

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  3. phpcms v9调用自定义字段的方法步骤

    代码如下:{loop $shigongtu $r}<img src="{$r[url]} " title="测试"/>{/loop} 2 首页,分页 ...

  4. LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16 Debug/firstapi.exe :

    #include <windows.h> #include <iostream.h> void main() { char SourceFileName[MAX_PATH]; ...

  5. maven项目和普通项目转换

     

  6. grunt学习笔记

    1. 在使用grunt前需要执行的几条命令和用途 npm uninstall -g grunt  删除掉全局grunt npm install -g grunt-cli 把grunt加入你的系统搜索路 ...

  7. Fragment全解析系列(一):那些年踩过的坑

    开始之前 最新版知乎,单Activity多Fragment的架构,响应可以说非常"丝滑",非要说缺点的话,就是没有转场动画,并且转场会有类似闪屏现象.我猜测可能和Fragment转 ...

  8. 【前端】使用readline模块实现Node.js的输入输出

    'use strict'; function f(x) { // do something... } var readline = require('readline'); //创建readline接 ...

  9. Xcode 8 Swift 类似插件方法

    Xcode8 Swift使用技巧 1 option + cmd + / 可以弹出注释 2 color 然后加 enter 可以弹出颜色选择 3    #FIXME:  警告 4   #MARK: 备注 ...

  10. Win10 + Nginx 1.10 + PHP 7 + Redis 配置方法

    软件包版本 软件 版本 - 链接 - Nginx nginx-x32-1.10.2.zip 下载 PHP php-7.0.12-nts-Win32-VC14-x64 下载 Redis php_redi ...