CodeForces - 711B  Chris and Magic Square

  题意:给你一个N*N矩阵,其中0代表未知的那个数,让你在这个位置填上一个数使整个矩阵的 每一行、每一列、主对角线、次对角线的和都相等。
 
  思路:一开始一直没理解题意,到底是行和=列和=对角线和,还是所以行的和相等,所有列的和相等,两条对角线的和相等。看到了are单词,明白了是第一种情况,之后就是简单的模拟了,特别判断n为1的情况,一定要注意细节,仔细仔细再仔细
#include<iostream>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
ll r[505],c[505],angle[3];
ll a[505][505];
int main(void)
{int n;cin>>n;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>a[i][j];if(n==1){cout<<1<<endl;return 0;}int u,v;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){r[i]+=a[i][j];c[i]+=a[j][i];if(a[i][j]==0){u=i;v=j;}if(i==j) angle[1]+=a[i][j];if((i+j)==(n+1)) angle[2]+=a[i][j];}int flag=1;ll R;   for(int i=1;i<=n;i++){if(i==u) continue;if(flag){R=r[i];flag=0;continue;}if(r[i]!=R){cout<<-1<<endl;return 0;}}ll C;flag=1;for(int i=1;i<=n;i++){if(i==v) continue;if(flag){C=c[i];flag=0;continue;}if(c[i]!=C){cout<<-1<<endl;return 0;       }}if(R!=C){cout<<-1<<endl;return 0;}if(u!=v&&(u+v)!=(n+1)){if(angle[1]==angle[2]&&angle[2]==R&&r[u]==c[v]&&R-r[u]>=1&&R-r[u]<=INF){cout<<R-r[u]<<endl;return 0;}else{cout<<-1<<endl;return 0;}}if(u==v&&(u+v)!=(n+1)){if(r[u]==c[v]&&angle[1]+R-r[u]==angle[2]&&angle[2]==R&&R-r[u]>=1&&R-r[u]<=INF){cout<<R-r[u]<<endl;return 0;}else{cout<<-1<<endl;return 0;}}if(u!=v&&(u+v)==(n+1)){if(r[u]==c[v]&&angle[2]+R-r[u]==angle[1]&&angle[1]==R&&R-r[u]>=1&&R-r[u]<=INF){cout<<R-r[u]<<endl;return 0;}else {cout<<-1<<endl;return 0;}}if(u==v&&(u+v)==(n+1)){if(r[u]==c[v]&&angle[2]==angle[1]&&angle[1]==c[v]&&R-r[u]>=1&&R-r[u]<=INF){cout<<R-r[u]<<endl;return 0;}else{cout<<-1<<endl;return 0;}}return 0;
}

CodeForces - 711B Chris and Magic Square相关推荐

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

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

  2. Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. cf369 B Chris and Magic Square

    B. Chris and Magic Square time limit per test2 seconds memory limit per test256 megabytes inputstand ...

  4. B. Chris and Magic Square

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  5. Chris and Magic Square

    Chris and Magic Square ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There ...

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

  7. 文章标题 Chris and Magic Square

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  8. 【codeforces 711B】Chris and Magic Square

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

  9. Codeforces 711B- Chris and Magic Square

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

最新文章

  1. 韩国一周新增千例确诊,想用 AI 定位出咳嗽的人
  2. tensorflow随笔-简单CNN(卷积深度神经网络结构)
  3. java子类和父类实例_java中父类与子类之间的转换示例
  4. Swift - 触摸事件(点击,移动,抬起等)说明及用例
  5. CCF 2017-3-2 学生排队
  6. Python爬虫入门并不难,甚至入门也很简单
  7. Godot 导出 Android apk
  8. 《Redis视频教程》(p2)
  9. eclipse工具按键翻译
  10. 关于breadcrumb面包屑
  11. linux挂载40t硬盘,Centos支持40T磁盘阵列MD1200
  12. FFmpeg基础: 视频裁剪
  13. 京东移动端首页-流式布局
  14. 华为p10android版本,华为P10系列什么时候升级安卓8.0?官方给出答案
  15. CentOS7 查询版本/CPU/内存/硬盘容量等命令
  16. 《UNIX编程艺术》精彩语录
  17. JSON解析合集:你再也不用怕JSON解析了!!!
  18. 日本研制超音速小飞机或达音速2倍
  19. 6.2 网络钓鱼攻击
  20. iMeta | 第1卷第4期来自8个国家的20篇文章正式发布(2022.12)

热门文章

  1. 全志Linux下载工具,全志 Allwinner A20 机顶盒刷入原生 Debian
  2. R绘图笔记 | 小提琴图与漂亮的云雨图绘制
  3. c 调用python类_C调用Python解析Excel
  4. meterpreter会话渗透利用常用的32个命令归纳小结
  5. 惠普136wm显示无法连接服务器,136w打印机时连接无线时显示连接失败
  6. 谈谈智能穿戴设备最火主控芯片NRF51422/NRF51822的开发感受
  7. 美狐美颜SDK趣拍功能的算法代码解析
  8. 3RA生态之定时器输出PWM
  9. Arduino工程源码分析
  10. 为什么安监控需要公网ip_视频监控系统分布式集中监控对宽带有何要求?带宽得多大?...