codeforces #369div2 B. Chris and Magic Square
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等
题目链接: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的更多相关文章
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- 【codeforces 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- 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 ...
- B. Chris and Magic Square
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
随机推荐
- QT QML目录导航列表视图
[功能] /目录.文件 /文件过滤 /递归 /事件 /高亮当前行 /当前选项 /目录切换动画 /限制根目录 [下载]:http://download.csdn.net/detail/surfsky/8 ...
- C# 获取当前路径7种方法
//获取模块的完整路径. string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //获取 ...
- PHP 笔记一(systax/variables/echo/print/Data Type)
PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP D ...
- angular.extend()和 angular.copy()的区别
1.angular.copy angular.copy(source, [destination]); // source: copy的对象. 可以使任意类型, 包括null和undefined. ...
- iOS设计规范整理|汇总
来源 UI中国
- QM模块包含主数据(Master data)和功能(functions)
QM模块包含主数据(Master data)和功能(functions) QM主数据 QM主数据 1 Material Master MM01/MM02/MM50待测 物料主数据 2 Sa ...
- jquery 获取元素背景图片backgroungImage的url
$("#").css("backgroundImage").replace('url(','').replace(')','');
- C++ 中注意,零扩展和符号位扩展
版权声明:本文为博主原创文章,未经博主允许不得转载. 首先,介绍一下两种扩展的定义 转 http://blog.csdn.net/jaylong35/article/details/6160736 符 ...
- rewrite规则中参数多于9个的处理方式 apache nginx
RewriteRule ^index-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)(.*)$ $9&a ...
- NPOI导入xls,xlsx格式实例
NPOI DLL下载地:http://npoi.codeplex.com/releases using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; us ...