Problem p00315 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2017/08/01 Tazoe

#include <iostream>
using namespace std;

bool check(bool P[1000][1000], int N) {
  for (int y = 0; y < N / 2; y++) {
    for (int x = 0; x < N / 2; x++) {
      if (P[y][x] == P[y][N - 1 - x] && P[y][x] == P[N - 1 - y][x] &&
          P[y][x] == P[N - 1 - y][N - 1 - x])
        continue;

      return false;
    }
  }

  return true;
}

int main() {
  int C, N;
  cin >> C >> N;

  bool P[1000][1000];
  for (int y = 0; y < N; y++) {
    for (int x = 0; x < N; x++) {
      char p;
      cin >> p;

      P[y][x] = (bool)(p - '0');
    }
  }
  /*
          for(int y=0; y<N; y++){
                  for(int x=0; x<N; x++){
                          cout << (int)P[y][x];
                  }
                  cout << endl;
          }
  */

  bool Q[1000][1000];
  int sumQ = 0;
  for (int y = 0; y < N / 2; y++) {
    for (int x = 0; x < N / 2; x++) {
      Q[y][x] = !(P[y][x] == P[y][x]);
      Q[y][N - 1 - x] = !(P[y][N - 1 - x] == P[y][x]);
      Q[N - 1 - y][x] = !(P[N - 1 - y][x] == P[y][x]);
      Q[N - 1 - y][N - 1 - x] = !(P[N - 1 - y][N - 1 - x] == P[y][x]);
      if (Q[y][x])
        sumQ++;
      if (Q[y][N - 1 - x])
        sumQ++;
      if (Q[N - 1 - y][x])
        sumQ++;
      if (Q[N - 1 - y][N - 1 - x])
        sumQ++;
    }
  }

  //	cout << sumQ << endl;

  int cnt = 0;

  if (sumQ == 0) {
    cnt++;
  }

  for (int i = 1; i < C; i++) {
    int D;
    cin >> D;

    for (int j = 0; j < D; j++) {
      int r, c;
      cin >> r >> c;

      if (r <= N / 2 && c <= N / 2) {
        Q[r - 1][N - c] = !Q[r - 1][N - c];
        Q[N - r][c - 1] = !Q[N - r][c - 1];
        Q[N - r][N - c] = !Q[N - r][N - c];
        if (Q[r - 1][N - c])
          sumQ++;
        else
          sumQ--;
        if (Q[N - r][c - 1])
          sumQ++;
        else
          sumQ--;
        if (Q[N - r][N - c])
          sumQ++;
        else
          sumQ--;
      } else {
        Q[r - 1][c - 1] = !Q[r - 1][c - 1];
        if (Q[r - 1][c - 1])
          sumQ++;
        else
          sumQ--;
      }
    }

    if (sumQ == 0) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

// 2017/08/01 Tazoe

#include <iostream>
using namespace std;

bool check(bool P[1000][1000], int N) {
  for (int y = 0; y < N / 2; y++) {
    for (int x = 0; x < N / 2; x++) {
      if (P[y][x] == P[y][N - 1 - x] && P[y][x] == P[N - 1 - y][x] &&
          P[y][x] == P[N - 1 - y][N - 1 - x])
        continue;

      return false;
    }
  }

  return true;
}

int main() {
  int C, N;
  cin >> C >> N;

  bool P[1000][1000];
  for (int y = 0; y < N; y++) {
    for (int x = 0; x < N; x++) {
      char p;
      cin >> p;

      P[y][x] = (bool)(p - '0');
    }
  }
  /*
          for(int y=0; y<N; y++){
                  for(int x=0; x<N; x++){
                          cout << (int)P[y][x];
                  }
                  cout << endl;
          }
  */

  int cnt = 0;

  if (check(P, N)) {
    cnt++;
  }

  for (int i = 1; i < C; i++) {
    int D;
    cin >> D;

    for (int j = 0; j < D; j++) {
      int r, c;
      cin >> r >> c;

      P[r - 1][c - 1] = !P[r - 1][c - 1];
    }

    if (check(P, N)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}