zoukankan      html  css  js  c++  java
  • 【模拟】Codeforces 711B Chris and Magic Square

    题目链接:

      http://codeforces.com/problemset/problem/711/B

    题目大意:

      N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么数。无解输出-1。幻方是每一行每一列和两条主对角线的和都相等。

    题目思路:

      【模拟】

      题目没看清外加爆intWA了好多次。。罪过。

      求出每一行每一列和对角线的和,为0的那个用随意一行的和扣去0的那一行就可以得到。只要验证其余的是否都相等即可。

      1 //
      2 //by coolxxx
      3 //#include<bits/stdc++.h>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<string>
      7 #include<iomanip>
      8 #include<map>
      9 #include<stack>
     10 #include<queue>
     11 #include<set>
     12 #include<bitset>
     13 #include<memory.h>
     14 #include<time.h>
     15 #include<stdio.h>
     16 #include<stdlib.h>
     17 #include<string.h>
     18 //#include<stdbool.h>
     19 #include<math.h>
     20 #define min(a,b) ((a)<(b)?(a):(b))
     21 #define max(a,b) ((a)>(b)?(a):(b))
     22 #define abs(a) ((a)>0?(a):(-(a)))
     23 #define lowbit(a) (a&(-a))
     24 #define sqr(a) ((a)*(a))
     25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
     26 #define mem(a,b) memset(a,b,sizeof(a))
     27 #define eps (1e-8)
     28 #define J 10
     29 #define mod 1000000007
     30 #define MAX 0x7f7f7f7f
     31 #define PI 3.14159265358979323
     32 #define N 504
     33 using namespace std;
     34 typedef long long LL;
     35 int cas,cass;
     36 int n,m,lll,ans;
     37 LL a[N][N];
     38 LL sum[N],tot[N];
     39 int sx,sy;
     40 bool judge()
     41 {
     42     int i,j;
     43     LL ll,c;
     44     if(sx==1)
     45         a[sx][sy]=sum[2]-sum[sx];
     46     else a[sx][sy]=sum[1]-sum[sx];
     47     sum[sx]+=a[sx][sy],tot[sy]+=a[sx][sy];
     48     c=sum[1];
     49     for(i=1;i<=n;i++)
     50         if(sum[i]!=c)
     51             return 0;
     52     for(j=1;j<=n;j++)
     53         if(tot[j]!=c)
     54             return 0;
     55     for(i=1,ll=0;i<=n;i++)
     56         ll+=a[i][i];
     57     if(ll!=c)return 0;
     58     for(i=1,ll=0;i<=n;i++)
     59         ll+=a[i][n+1-i];
     60     if(ll!=c)return 0;
     61     return 1;
     62 }
     63 int main()
     64 {
     65     #ifndef ONLINE_JUDGE
     66 //    freopen("1.txt","r",stdin);
     67 //    freopen("2.txt","w",stdout);
     68     #endif
     69     int i,j,k;
     70     
     71 //    for(scanf("%d",&cass);cass;cass--)
     72 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
     73 //    while(~scanf("%s",s+1))
     74     while(~scanf("%d",&n))
     75     {
     76         mem(sum,0);mem(tot,0);
     77         for(i=1;i<=n;i++)
     78         {
     79             for(j=1;j<=n;j++)
     80             {
     81                 scanf("%I64d",&a[i][j]);
     82                 if(a[i][j]==0)sx=i,sy=j;
     83                 sum[i]+=a[i][j];
     84                 tot[j]+=a[i][j];
     85             }
     86         }
     87         if(n==1)
     88         {
     89             puts("1");
     90             continue;
     91         }
     92         if(judge() && a[sx][sy]>0)printf("%I64d
    ",a[sx][sy]);
     93         else puts("-1");
     94     }
     95     return 0;
     96 }
     97 /*
     98 //
     99 
    100 //
    101 */
    View Code
  • 相关阅读:
    JAVA多线程与并发学习总结
    Java虚拟机类加载机制
    2013网易校园招聘笔试题
    人人网面试题
    2010人人网校园招聘笔试题
    2011人人网校园招聘笔试题
    2012人人网校园招聘笔试题
    2013人人网校园招聘笔试题
    海量数据查找唯一数据问题
    Hulu面试题
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5822021.html
Copyright © 2011-2022 走看看