题目链接:

B. Chris and Magic Square

题意:

问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好;

AC代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <bits/stdc++.h>
  7. #include <stack>
  8. #include <map>
  9.  
  10. using namespace std;
  11.  
  12. #define For(i,j,n) for(int i=j;i<=n;i++)
  13. #define mst(ss,b) memset(ss,b,sizeof(ss));
  14.  
  15. typedef long long LL;
  16.  
  17. template<class T> void read(T&num) {
  18. char CH; bool F=false;
  19. for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
  20. for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
  21. F && (num=-num);
  22. }
  23. int stk[70], tp;
  24. template<class T> inline void print(T p) {
  25. if(!p) { puts("0"); return; }
  26. while(p) stk[++ tp] = p%10, p/=10;
  27. while(tp) putchar(stk[tp--] + '0');
  28. putchar('\n');
  29. }
  30.  
  31. const LL mod=1e9+7;
  32. const double PI=acos(-1.0);
  33. const int inf=1e9;
  34. const int N=4e5+10;
  35. const int maxn=1e3+520;
  36. const double eps=1e-12;
  37.  
  38. LL mp[505][505],l[505],r[505];
  39. int main()
  40. {
  41. int n,x=0,y=0;
  42. read(n);
  43. For(i,1,n)
  44. {
  45. r[i]=0;
  46. For(j,1,n)
  47. {
  48. read(mp[i][j]);
  49. if(mp[i][j]==0)
  50. {
  51. x=i;y=j;
  52. }
  53. r[i]+=mp[i][j];
  54. }
  55. }
  56. if(n==1)
  57. {
  58. cout<<"1\n";
  59. return 0;
  60. }
  61. int num=0;
  62. for(int i=2;i<=n;i++)
  63. {
  64. if(r[i]!=r[i-1])num++;
  65. }
  66. if(num>2)cout<<"-1\n";
  67. else
  68. {
  69. LL temp;
  70. if(x>1)temp=r[1];
  71. else temp=r[2];
  72. LL sum=temp;
  73. for(int i=1;i<=n;i++)
  74. {
  75. if(mp[x][i])temp-=mp[x][i];
  76. }
  77. mp[x][y]=temp;
  78. if(temp<=0)cout<<"-1\n";
  79. else
  80. {
  81. for(int i=1;i<=n;i++)
  82. {
  83. l[i]=0;
  84. for(int j=1;j<=n;j++)
  85. {
  86. l[i]+=mp[j][i];
  87. }
  88. }
  89. for(int i=2;i<=n;i++)
  90. {
  91. if(l[i]!=sum)
  92. {
  93. cout<<"-1\n";
  94. return 0;
  95. }
  96. }
  97. temp=0;
  98. for(int i=1;i<=n;i++)
  99. {
  100. temp+=mp[i][i];
  101. }
  102. if(temp!=sum)
  103. {
  104. cout<<"-1\n";
  105. return 0;
  106. }
  107. temp=0;
  108. for(int i=1;i<=n;i++)
  109. {
  110. temp+=mp[i][n-i+1];
  111. }
  112. if(temp!=sum)
  113. {
  114. cout<<"-1\n";
  115. return 0;
  116. }
  117. cout<<mp[x][y]<<endl;
  118. }
  119. }
  120.  
  121. return 0;
  122. }

  

codeforces 711B B. Chris and Magic Square(水题)的更多相关文章

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

  2. 【codeforces 711B】Chris and Magic Square

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

  3. Xtreme8.0 - Magic Square 水题

    Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...

  4. codeforces #369div2 B. Chris and Magic Square

    题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...

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

  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. B. Chris and Magic Square

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

  8. codeforces Gym 100187L L. Ministry of Truth 水题

    L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  9. Codeforces Round #185 (Div. 2) B. Archer 水题

    B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...

随机推荐

  1. U-boot的环境变量值得注意的有两个: bootcmd 和bootargs

    本文转载至:http://www.cnblogs.com/cornflower/archive/2010/03/27/1698279.html U-boot的环境变量值得注意的有两个: bootcmd ...

  2. 在Android设备上判断设备是否支持摄像头

    private boolean hasCamera(){ boolean hasCamera=false; PackageManager pm=getActivity().getPackageMana ...

  3. java jdk environment variables

    1. create system variable 2. edit the system path note: ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 3. cre ...

  4. PHP学习笔记:keditor的使用

    keditor时一个免费的开源编辑器,很多公司在使用(百度编辑器也不错).最近为了做一个客户信息管理系统,在发送邮件模块用到这个编辑器,也算学习一下新的东西. 第一步:下载编辑器 到它的官网下载:ht ...

  5. PHP学习笔记:对命名空间(namespace)学习资料的翻译

    Name collisions means: you create a function named db_connect, and somebody elses code that you use ...

  6. 开源项目Foq简介

        Foq是一个轻量级-线程安全的mocking类库.使用它来mock抽象类与接口这是我们通常的做法.Foq的名字来自Moq,如果你使用过Moq的话,自然后联想到它能做什么.Foq主要是为了F#的 ...

  7. 每日微软面试题——day 6(打印所有对称子串)

    每日微软面试题——day 6(打印所有对称子串) 分类: 2.数据结构与算法2011-08-14 14:27 9595人阅读 评论(15) 收藏 举报 面试微软string测试systemdistan ...

  8. Android自动更新安装后显示‘完成’‘打开’按钮

    /** * 安装apk * * @param url */ private void installApk() { File apkfile = new File(apkFilePath); if ( ...

  9. 弃用的异步get和post方法之代理方法

    #import "ViewController.h" #import "Header.h" @interface ViewController () <N ...

  10. java调用python代码

    同样的我们需要安装jython,具体的步骤如下: 1. 去 http://sourceforge.net/projects/jython/ 下载最新的jython相关的jar包. 2. 下载下来的ja ...