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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int MAX_N = 1005;

int N, E[MAX_N][MAX_N], V[MAX_N][MAX_N], f = 0;

void init() {
  memset(E, 0, sizeof(E));
  E[0][0] = V[0][0];
  FOR(i, 1, N) E[i][0] = E[i - 1][0] + V[i][0];
  FOR(i, 1, N) E[0][i] = E[0][i - 1] + V[0][i];
  FOR(y, 1, N)
  FOR(x, 1, N)
  E[y][x] = V[y][x] + E[y - 1][x] + E[y][x - 1] - E[y - 1][x - 1];
}

bool check(int Y1, int X1, int Y2, int X2) {
  int cnt = E[Y2][X2];
  if (X1 - 1 >= 0)
    cnt -= E[Y2][X1 - 1];
  if (Y1 - 1 >= 0)
    cnt -= E[Y1 - 1][X2];
  if (X1 - 1 >= 0 && Y1 - 1 >= 0)
    cnt += E[Y1 - 1][X1 - 1];
  return (cnt == 0 ? true : false);
}

int solve() {
  for (int i = N - 1; i > 0; --i)
    REP(y, N - i) REP(x, N - i) if (check(y, x, y + i, x + i)) return i + 1;
  return (int)f;
}

int main() {
  while (scanf("%d", &N) && N) {
    f = 0;
    REP(i, N) {
      string s;
      cin >> s;
      REP(j, s.length()) {
        V[i][j] = (s[j] == '*' ? 1 : 0);
        if (V[i][j] == 0)
          f = 1;
      }
    }
    init();
    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int MAX_N = 1005;

int N, E[MAX_N][MAX_N], V[MAX_N][MAX_N];

void init() {
  memset(E, 0, sizeof(E));
  E[0][0] = V[0][0];
  FOR(i, 1, N) E[i][0] = E[i - 1][0] + V[i][0];
  FOR(i, 1, N) E[0][i] = E[0][i - 1] + V[0][i];
  FOR(y, 1, N)
  FOR(x, 1, N)
  E[y][x] = V[y][x] + E[y - 1][x] + E[y][x - 1] - E[y - 1][x - 1];
}

bool check(int Y1, int X1, int Y2, int X2) {
  int cnt = E[Y2][X2];
  if (X1 - 1 >= 0)
    cnt -= E[Y2][X1 - 1];
  if (Y1 - 1 >= 0)
    cnt -= E[Y1 - 1][X2];
  if (X1 - 1 >= 0 && Y1 - 1 >= 0)
    cnt += E[Y1 - 1][X1 - 1];
  return (cnt == 0 ? true : false);
}

int main() {
  while (scanf("%d", &N) && N) {
    REP(i, N) {
      string s;
      cin >> s;
      REP(j, s.length()) V[i][j] = (s[j] == '*' ? 1 : 0);
    }
    init();
    int ans = 0;
    REP(y, N)
    REP(x, N) REP(i, min(N - y, N - x)) if (check(y, x, y + i, x + i)) ans =
        max(ans, i + 1);
    cout << ans << endl;
  }
  return 0;
}