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:38:35
Task:Grid
Sender:TreamDeam
Submission time:2018-05-26 12:38:35
Status:READY
Result:ACCEPTED

Show test data

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:12:9: warning: unused variable 'ai' [-Wunused-variable]
     int ai;
         ^~

Code

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;

    int ar[n][n];
    int visited[n][n];
    char a;
    int ai;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a;
            int ai = a - '0';
            ar[i][j] = ai;
            visited[i][j] = 0;
        }
    }

    int ans = 0;

    for (int i = 0; i< n; i++) {
        int all = 1;
        for (int j=0; j < n; j++){
            if (ar[i][j] == 0) {
                all = 0;
                break;
            }
        }
        if (all) {
            ans++;
            for (int k = 0; k < n; k++) {
                visited[i][k] = 1;
            }
        }
    }

    for (int i = 0; i< n; i++) {
        int all = 1;
        for (int j=0; j < n; j++){
            if (ar[j][i] == 0) {
                all = 0;
                break;
            }
        }
        if (all) {
            ans++;
            for (int k = 0; k < n; k++) {
                visited[k][i] = 1;
            }
        }
    }

    int all2 = 1;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++) {
            if (visited[i][j] == 0 && ar[i][j] == 1 ) {
                all2 = 0;
                break;
            }
        }
        if (all2 == 0) {
            break;
        }
    }

    if (all2 == 0) {
        cout << "-1\n";
    } else {
        if (ans == 2*n) {
            ans = n;
        }
        cout << ans << endl;
    }

    return 0;
}