Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 12:06:04
Task:Grid
Sender:HIIT AND RUN
Submission time:2018-05-26 12:06:04
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int v[1000][1000];
int rivit[1000];
int sarakkeet[1000];

int main() {
    int n;
    cin >> n;
    
    
    
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < n; j++) {
            v[i][j] = ((int) s[j]) - 48;
        }
    }
    
    
    for (int i = 0; i < n; i++) {
        
        int summa = 0;
        for (int j = 0; j < n; j++) {
            summa += v[i][j];
        }
        rivit[i] = (summa == n);
    }
    
    for (int i = 0; i < n; i++) {
        
        int summa = 0;
        for (int j = 0; j < n; j++) {
            summa += v[j][i];
        }
        sarakkeet[i] = (summa == n);
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (rivit[i] == 0 && sarakkeet[j] == 0 && v[i][j] == 1) {
                cout << "-1" << endl;
                exit(0);
            }
        }
    }
    
    ll sarakesumma = 0;
    
    for (int i = 0; i < n; i++) {
        sarakesumma += sarakkeet[i];
    }
    
    ll rivisumma = 0;
    
    for (int i = 0; i < n; i++) {
        rivisumma += rivit[i];
    }
    
    
    
    ll total = sarakesumma + rivisumma;
    
    if (total == 2 * n) {
        cout << n << endl;
        return 0;
    }
    
    cout << total << endl;
}