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:02:22
2018-05-26 12:00:44
2018-05-26 11:57:56
Task:Grid
Sender:Lucinda
Submission time:2018-05-26 12:02:22
Status:READY
Result:ACCEPTED

Show test data

Code

#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;
    char c;
    vector<bool> m(n * n, false);
    vector<bool> rows(n, true); // black rows

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> c;
            if (c == '1') {
                m[i * n + j] = true;
            } else {
                rows[i] = false;
            }
        }
    }

    if (n == 1) {
        if (m[0]) cout << 1 << endl;
        else cout << 0 << endl;
        return 0;
    }

    int blackColCount = 0;
    
    for (int j = 0; j < n; j++) {
        bool isBlack = true;
        bool isValid = true;
        for (int i = 0; i < n; i++) {
            if (m[i * n + j]) {
                if (!rows[i]) isValid = false;
            } else {
                isBlack = false;
            }
        }
        if (!isBlack && !isValid) {
            cout << -1 << endl;
            return 0;
        }
        if (isBlack) blackColCount++;
    }
    if (blackColCount == n) {

        cout << n << endl;
        return 0;
    }

    int blackRowCount = 0;

    for (int i = 0; i < n; i++) {
        if (rows[i]) blackRowCount++;
    }
    cout << blackColCount + blackRowCount << endl;
}