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

题目链接: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. java 入门学习

    想要学习java,首先你要明白java是干嘛的,它有什么吸引之处,懂程序的都应该知道,java是很多计算机语言的根本,无论在什么时代,科技如何更新,java都不会落后,现在的我在学习初级java,下面 ...

  2. Eclipse 的快捷键以及文档注释、多行注释的快捷键

    一.多行注释快捷键 1.选中你要加注释的区域,用ctrl+shift+C 或者ctrl+/ 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/*    */注释 3.以上快捷 ...

  3. MongoDB由于目标计算机积极拒绝,无法连接

    遇到这个问题的时候,可以通过以下步骤解决: 1.打开Mongo安装包:进入Mongo下的data文件夹下的db文件夹,找到Mongod.lock,删除. 2.在命令行中输入: mongod.exe - ...

  4. PowerShell添加或修改注册表开机启动项脚本

    代码如下: $name = Read-Host "请输入开机启动项的名字(随便起)" $value = Read-Host "请输入开机启动项的值" try{ ...

  5. 学习日志 - Openwrt安装python然后wallproxy

    前提: - 先要把U盘插入路由器的usb口,大多数情况Openwrt都会自动挂载的吧,尽量找当前年或前一年的固件.ssh进路由器,可以看到/mnt/sda1 -  让路由器联网,因为需要从网络上下载安 ...

  6. Symbol not found for architecture arm64 错误

    如果Archiectures和Valid Architectures都是对的,需要检查Other Linker Flags是否添加$(inherited),如果还不行,检查Library Search ...

  7. java正则表达式【大全】

    [正则表达式]文本框输入内容控制整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$& ...

  8. LookUpEditPopup自动调整宽度

    this.Properties.BestFitMode = BestFitMode.BestFitResizePopup;

  9. 空间点绕轴旋转公式&程序(C++)

    关键词:空间旋转.旋转轴 用途:相机位姿估计.无人机位姿估计.3D游戏.3D建模 文章类型:概念.公式总结(本文不带推倒过程,若想了解公式是如何推出来的请搜索文献),C++函数展示 @Author:V ...

  10. SQL Server优化常用SQL语句

    --所有没有主键的表 select name from sysobjects where xtype='U' and id not in ( select i.parent_obj from syso ...