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:16:10
Task:Grid
Sender:Puhi~
Submission time:2018-05-26 12:16:10
Status:READY
Result:ACCEPTED

Show test data

Code

#include <iostream>

using namespace std;

bool rowset[1000];
bool colset[1000];

int main() {
    int n;
    cin >> n;
    
    bool grid[1000][1000] = {0};
    
    for (int x = 0; x < n; x++) {
        rowset[x] = true;
        colset[x] = true;
    }
    
    for (int x = 0; x < n; x++) {
        for(int y = 0; y < n; y++) {
            char tmp;
            cin >> tmp;
            if (tmp == '0') {
                colset[x] = false;
                rowset[y] = false;
            } else {
                grid[x][y] = true;
            }
        }
    }
    
    bool allset = true;
    for (int x = 0; x < n; x++) {
        if (!rowset[x]) allset = false;
    }
    
    if (allset) {
        cout << n << endl;
    } else {
        for (int x = 0; x < n; x++) {
            for(int y = 0; y < n; y++) {
                if (grid[x][y] && !rowset[y] && !colset[x]) {
                    cout << -1 << endl;
                    return 0;
                }
            }
        }
        
        int count = 0;
        for (int x = 0; x < n; x++) {
            count += rowset[x] + colset[x];
        }
        cout << count << endl;
    }
    
    return 0;
}