• CodeForce 711B -- Chris and Magic Square


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

      ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.

      Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal — and the secondary diagonal — ) are equal.

    Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

    Input

      The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

    n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.

    It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.

    Output

      Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

    If there are multiple solutions, you may print any of them.

    Examples
     
    Input
     
    3
    4 0 2
    3 5 7
    8 1 6
    Output
     
    9
    Input
    4
    1 1 1 1
    1 1 0 1
    1 1 1 1
    1 1 1 1
    Output
    1
    Input
    4
    1 1 1 1
    1 1 0 1
    1 1 2 1
    1 1 1 1
    Output
    -1
    Note

    In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,

    The sum of numbers in each row is:

    4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.

    The sum of numbers in each column is:

    4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.

    The sum of numbers in the two diagonals is:

    4 + 5 + 6 = 2 + 5 + 8 = 15.

    In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.

    题意:

      给你一个矩阵,上面有只有一个数字没有填,问这个位置填多少,能使每一行,每一列,两个主对角线相等,有就输出这个数,没有输出0

    思路:

      模拟题

    AC代码:

      1 # include <bits/stdc++.h>
      2 using namespace std;
      3 typedef  long long int ll;
      4 ll num[510][510];
      5 int main()
      6 {
      7     int n;
      8     cin >> n;
      9     memset(num, 0, sizeof(num));
     10     int h = -1, l = -1, dui = 0;
     11     ll sum1 = 0;
     12     ll sum2 = 0;
     13     int flag = 0;
     14     for(int i = 1; i <= n; i++)
     15     {    
     16         for(int j = 1; j <= n; j++)
     17         {
     18             scanf("%I64d", &num[i][j]);
     19             if(num[i][j] == 0)
     20             {
     21                 h = i;
     22                 l = j;
     23             }
     24             num[i][0] += num[i][j];
     25             num[0][j] += num[i][j];
     26             if(i == j)
     27             {
     28                 sum1 += num[i][j];
     29                 if(h == i && l == j)
     30                 {
     31                     flag = 1;
     32                     dui++;
     33                 }
     34             }
     35             if(i == (n - j + 1))
     36             {
     37                 sum2 += num[i][j];
     38                 if(h == i && l == j)
     39                 {
     40                     flag = 2;
     41                     dui++;
     42                 }
     43             }
     44         }
     45     }
     46     if(n == 1)
     47     {
     48         cout << "1" << endl;
     49         return 0;
     50     }
     51     int haha = 1;
     52     ll tmp;
     53     for(int i = 1; i <= n; i++)
     54     {
     55         if(h != i)
     56             tmp = num[i][0];
     57     }
     58     for(int i = 1; i <= n; i++)
     59     {
     60         if(h != i && tmp != num[i][0])
     61             haha = 0;
     62     }
     63     ll tmp1;
     64     for(int i = 1; i <= n; i++)
     65     {
     66         if(l != i)
     67             tmp1 = num[0][i];
     68     }
     69     for(int i = 1; i <= n; i++)
     70     {
     71         if(l != i && tmp1 != num[0][i])
     72             haha = 0;
     73     }
     74     if(tmp != tmp1)
     75         haha = 0;
     76     if((tmp - num[h][0] != tmp1 - num[0][l]) || (tmp - num[h][0] <= 0))
     77         haha = 0;
     78     
     79     if(dui == 2)
     80     {
     81         if(sum1 != sum2)
     82             haha = 0;
     83         if((tmp - num[h][0]) != tmp - sum1)
     84             haha = 0;
     85     }
     86     else if(flag == 1)
     87     {
     88         if(sum2 != tmp)
     89             haha = 0;
     90         if(sum2 - sum1 != tmp - num[h][0])
     91             haha = 0;
     92     }
     93     else if(flag == 2)
     94     {
     95         if(sum1 != tmp)
     96             haha = 0;
     97         if(sum1 - sum2 != tmp - num[h][0])
     98             haha = 0;
     99         
    100     }
    101     else
    102     {
    103         if(sum1 != sum2 || sum1 != tmp)
    104             haha = 0;
    105     }
    106     
    107     if(haha)
    108         cout << tmp - num[h][0] << endl;
    109     else
    110         cout << "-1" << endl;
    111     return 0;
    112 }
    View Code
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    解决:打开OleView报错 dllregisterserver in iviewers failed
    VS中查看/修改Dialog控件TAB顺序的方法
    如何调试DLL组件
    【转】解决:fatal error C1083: 无法打开预编译头文件
    Oracle ROWNUM用法和分页查询总结
    ORACLE的rownum用法讲解
    LINUX重启MYSQL的命令
    电脑资源管理器被关闭怎么启动
    Microsoft Active Directory(LDAP)连接常见错误代码
    IntelliJ IDEA 常用设置
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5820435.html
Copyright © 2020-2023  润新知