fork download
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <deque>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <ctime>
  20. #include <cstring>
  21. #include <math.h>
  22. #define lli long long int
  23.  
  24.  
  25.  
  26. using namespace std;
  27.  
  28. int main(){
  29. int n;
  30. cin >> n;
  31. lli a[n][n];
  32. int i = 0;
  33. int j = 0;
  34. int x, y;
  35. lli xsum = 0;
  36. lli ysum = 0;
  37. lli diag1 = 0;
  38. lli diag2 = 0;
  39. lli checksum = 0;
  40. for(int i = 0; i < n; i++){
  41. for(int j = 0; j < n; j++){
  42. cin >> a[i][j];
  43. if(a[i][j] == 0){
  44. x = i;
  45. y = j;
  46. }
  47. }
  48. }
  49. for(int j = 0; j < n; j++){
  50. xsum += a[x][j];
  51. checksum += a[x+1][j];
  52. }
  53. for(int j = 0; j < n; j++){
  54.  
  55. ysum += a[j][y];
  56. }
  57. int f = n-1;
  58. int t = 0;
  59. diag1 = a[0][0];
  60. diag2 = a[n-1][0];
  61. if(x == y){
  62. for(int i = 0; i < n; i++){
  63. for(int j = 1; j < n-1; j++){
  64. diag1 += a[i+1][j];
  65. }
  66.  
  67. }
  68.  
  69. if(diag1 == xsum && xsum == ysum){
  70. cout << abs(xsum - checksum);
  71. return 0;
  72. }
  73. else{
  74. cout << -1;
  75. return 0;
  76. }
  77. }
  78. else if(abs(f-y) == abs(x-t)){
  79. for(int i = n-1; i > 1; i--){
  80. for(int j = 1; j < n-2; j++){
  81. diag2 += a[i][j];
  82. }
  83. }
  84.  
  85. if(diag2 == xsum && xsum == ysum){
  86. cout << abs(xsum - checksum);
  87. return 0;
  88. }
  89. else{
  90. cout << -1;
  91. return 0;
  92. }
  93. }
  94. else{
  95. if(xsum == ysum){
  96. cout << abs(checksum - xsum) << endl;
  97.  
  98. }
  99.  
  100. }
  101.  
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
Success #stdin #stdout 0s 3472KB
stdin
3
4 0 2
3 5 7
8 1 6
stdout
9