Missing number in matrix

**

Missing number in matrix

**
matrix
Given a matrix A[][] of size N such that it has only one 0, Find the number to be placed in place of the 0 such that sum of the numbers in every row, column and two diagonals become equal.

Note: Diagonals should be only of the form A[i][i] and A[i][N-i-1]. If there is a matrix of size 1, with an element 0, then print 1.

Input:
First line contains the test cases, T . Then T test cases follow. Each test case contains a single integer N which represents the number of rows and columns of the matrix. Then in the next line are N*N space separated of the matrix A.

Output:
Display the number in place of 0 if possible, otherwise display -1. If there is a matrix of size 1, with an element 0, then print 1.

Constraints:

1<=T<=25
1<=n<=100
1<=ai<=1000000000

Example:
Input

2
2
5 5 5 0
3
1 2 0 3 1 2 2 3 1

Output:

5
-1

Explanation:

(i)In the first test case the matrix is

5 5
5 0

Therefore If we place 5 instead of 0, all the element of matrix will become 5. Therefore row 5+5=10, column 5+5=10 and diagonal 5+5=10, all are equal.

(ii)Second sample, it is not possible to insert an element in place of 0 so that the condition is satisfied.thus result is -1.

简单题,不解释,好久没练了,熟悉下手感。代码如下:

#include<iostream>
#include<string.h>
#include<vector>
//#include <cassert>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;

class Cite_Sum{
public:
    unsigned long long sum;
    bool Inf_Used;
    Cite_Sum(){sum=0;Inf_Used=0;}
    //bool operator == (const Cite_Sum & s);
};
bool operator == ( Cite_Sum & a,Cite_Sum & b)
{return a.sum==b.sum;}  
bool operator <( Cite_Sum& a,Cite_Sum& b){
    return a.sum<b.sum;
}
bool compare(Cite_Sum& a,Cite_Sum& b)
{
    return a.Inf_Used<b.Inf_Used;  
}
int main(){
    int Count,row,column;   
    cin>>Count; 
    while(Count--)
    {   int N;
        int Infu_num=2;
        cin>>N;
        if (N==1)
        {
            unsigned long c;
            cin>>c;
            if (c==0)
            cout<<"1"<<endl;
            else cout<<"-1"<<endl;
        }
        else
        {
            vector<Cite_Sum> Su(2*N+2);
            int ps=1;
           unsigned long **p=new unsigned long*[N];
           for (int i=0;i<N;i++)
             p[i]=new unsigned long[N];
           for(int i=0;i<N;i++)
          {    for(int j=0;j<N;j++)
             { cin>>p[i][j];
                if (p[i][j]==0)
                {
                   row=i;
                   column=j;
                   //行列对于哪些SUM有影响
                   Su[i].Inf_Used=1;
                   Su[j+N].Inf_Used=1;
                   if(i==N-j-1)
                   {  Su[2*N+1].Inf_Used=1;
                      Infu_num++;
                   }
                   if(i==j)
                   { Su[2*N].Inf_Used=1;
                      Infu_num++;
                   }
                 }     
               if(i==j)
               Su[2*N].sum+=p[i][j];
               if(i==N-j-1)            
               Su[2*N+1].sum+=p[i][j];
               Su[i].sum+= p[i][j];
               Su[j+N].sum+= p[i][j];
        }
          }
        unsigned long long s=Su[row].sum;

        if(Su[row].sum==Su[column+N].sum)
        {     
            ps=1;
            if(Su[2*N].Inf_Used==1&&Su[row].sum!=Su[2*N].sum)
                  ps=0;
            if(Su[2*N+1].Inf_Used==1&&Su[row].sum!=Su[2*N+1].sum)
                  ps=0; 
         }//被影响行列的SUM是否相等
          if (ps!=0)
          {
             //int s=Su[row].sum;
             vector<Cite_Sum>::iterator new_end;
             sort(Su.begin(),Su.end(),compare);
             while (Infu_num--)
             Su.pop_back();
             new_end=unique(Su.begin(),Su.end()); 
            //assert(Su.size()==N);
             Su.erase(new_end,Su.end());  
             int h=Su.size();
             if (h!=1) ps=0;
           }
           if (ps!=0&&Su[0].sum>s)
          {
            unsigned long long re=Su[0].sum-s;
              cout<<re<<endl;
           }
          else cout<<"-1"<<endl;
          for (int i=0;i<N;i++)
              delete p[i];
        }       
    }   
    return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值