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

【题意】



让你在矩阵中一个空白的地方填上一个正数;

使得这个矩阵两个对角线上的和;

每一行的和,每一列的和都相同;

【题解】



对于n=1的情况,任意输出一个数字就好;

对于n>1的情况;

先算出不包括空白格子的行的所有元素的和->he;

然后对于其他的可行的行和列,算出它们的和;

一旦与he不一样,直接输出-1无解;

然后包括空白格子的行和列,它们除了那个空白格子的和也要相同->设为sphe;

然后是对角线;

如果包括了空白格子,就先算出和;看看和sphe是不是一样;

如果不包括空白格子,算出的和直接与he比较;

两个对角线同理;

最后

sphe必须要小于he

即严格<不能相同

因为要为正数



【Number Of WA】



0



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define ps push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) cin >> x
  13. #define pri(x) cout << x
  14. #define ms(x,y) memset(x,y,sizeof x)
  15. typedef pair<int,int> pii;
  16. typedef pair<LL,LL> pll;
  17. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  18. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  19. const double pi = acos(-1.0);
  20. const int N = 510;
  21. LL a[N][N],hang[N],lie[N],he=-1;
  22. int n,sx,sy;
  23. int main()
  24. {
  25. //freopen("F:\\rush.txt","r",stdin);
  26. ios::sync_with_stdio(false);
  27. rei(n);
  28. if (n==1)
  29. return pri(1<<endl),0;
  30. rep1(i,1,n)
  31. rep1(j,1,n)
  32. {
  33. rei(a[i][j]);
  34. if (!a[i][j])
  35. sx = i,sy = j;
  36. }
  37. rep1(i,1,n)
  38. {
  39. LL temp = 0;
  40. rep1(j,1,n)
  41. temp+=a[i][j];
  42. hang[i] = temp;
  43. if (sx==i) continue;
  44. if (he==-1)
  45. he = temp;
  46. else
  47. if (he!=temp)
  48. return pri(-1<<endl),0;
  49. }
  50. rep1(j,1,n)
  51. {
  52. LL temp = 0;
  53. rep1(i,1,n)
  54. temp+=a[i][j];
  55. lie[j] = temp;
  56. if (sy==j) continue;
  57. if (he!=temp)
  58. return pri(-1<<endl),0;
  59. }
  60. bool in1 = false;
  61. LL he1 = 0;
  62. rep1(i,1,n)
  63. {
  64. if (i==sx && i==sy) in1 = true;
  65. he1+=a[i][i];
  66. }
  67. if (!in1 && he1!=he) return pri(-1<<endl),0;
  68. if (in1 && he1!=hang[sx]) return pri(-1<<endl),0;
  69. bool in2 = false;
  70. LL he2 = 0;
  71. rep1(i,1,n)
  72. {
  73. if (i==sx && n-i+1==sy) in2 = true;
  74. he2+=a[i][n-i+1];
  75. }
  76. if (!in2 && he2!=he) return pri(-1<<endl),0;
  77. if (in2 && he2!=hang[sx]) return pri(-1<<endl),0;
  78. if (hang[sx]<he)
  79. {
  80. pri(he-hang[sx]);
  81. }
  82. else
  83. pri(-1<<endl);
  84. //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
  85. return 0;
  86. }

【codeforces 711B】Chris and Magic Square的更多相关文章

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

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

  2. 【codeforces 255D】Mr. Bender and Square

    [题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...

  3. 【23.15%】【codeforces 703C】Chris and Road

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  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 水题

    B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...

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

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【硅谷问道】Chris Lattner 访谈录(下)

    [硅谷问道]Chris Lattner 访谈录(下) Chris Lattner 访谈录(下) 话题 Swift 在 Server 和操作系统方面有着怎样的雄心抱负? Swift 与 Objectiv ...

  9. 【硅谷问道】Chris Lattner 访谈录(上)

    [硅谷问道]Chris Lattner 访谈录(上) 话题 Chris Lattner 是谁? Xcode 的编译器 LLVM 背后有怎样的故事? Swift 诞生的前世今生,封闭的苹果为何要拥抱开源 ...

随机推荐

  1. Black Rock Shooter

    在人气动漫 Black Rock shooter 中,当加贺里对麻陶 说出了"滚回去"以后,与此同时,在另一个心灵世界里, BRS 也遭到了敌人的攻击.此时,一共有 n 个攻击排成 ...

  2. python lambda表达式&map/filter/reduce

    习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: 1 2 3 4 5 6 7 8 # 普通条件语句 if 1 == 1:     name = 'wupeiqi' else ...

  3. Java 删除List元素的正确方式

    方式一:使用Iterator的remove()方法 public class Test { public static void main(String[] args) { List<Strin ...

  4. skiing 暴力搜索 + 动态规划

    我的代码上去就是 直接纯粹的  暴力  .   居然没有超时   200ms  可能数据比较小   一会在优化 #include<stdio.h> #include<string.h ...

  5. 题解报告:hdu 3790 最短路径问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...

  6. UNIX环境高级编程--3

    文件IO 函数lseek: 每个打开文件都有一个与其相关联的“当前文件偏移量”,用来度量从文件开始处计算的字节数.除非指定O_APPEND选项,否则该偏移量被置为0.如果文件描述符指向的是一个管道.F ...

  7. 342 Power of Four 4的幂

    给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂.示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false.问题进阶:你能不使用循环/递归来解 ...

  8. [ USACO 2018 OPEN ] Out of Sorts (Platinum)

    \(\\\) \(Description\) 对一长为\(N\)的数列\(A\)排序,不保证数列元素互异: 数列\(A\)中\(A[1...i]\)的最大值不大于\(A[i+1-N]\)的最小值,我们 ...

  9. Android 串口驱动和应用测试

    这篇博客主要是通过一个简单的例子来了解Android的串口驱动和应用,为方便后续对Android串口服务和USB虚拟串口服务的了解.这个例子中,参考了<Linux Device Drivers& ...

  10. C#入门经典 Chapter4 流程控制

    4.1布尔逻辑 布尔比较运算符 ==  !=   <   >    <=    >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...