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

// 2011/03/18 Tazoe

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int H, W;
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;

    char rct[502][502];
    for (int i = 0; i <= H + 1; i++)
      for (int j = 0; j <= W + 1; j++)
        rct[i][j] = '*';

    for (int i = 1; i <= H; i++)
      for (int j = 1; j <= W; j++)
        cin >> rct[i][j];

    short wh[502][502][2];

    for (int i = 1; i <= H; i++)
      for (int w = 0, j = 1; j <= W; j++)
        if (rct[i][j] == '*')
          wh[i][j][0] = w = 0;
        else
          wh[i][j][0] = ++w;

    for (int j = 1; j <= W; j++)
      for (int h = 0, i = 1; i <= H; i++)
        if (rct[i][j] == '*')
          wh[i][j][1] = h = 0;
        else
          wh[i][j][1] = ++h;

    int rct_max = -1;
    for (int i = H; i >= 1; i--) {
      for (int j = W; j >= 1; j--) {
        if (wh[i][j][0] * wh[i][j][1] <= rct_max)
          continue;
        if (wh[i][j][0] > rct_max)
          rct_max = wh[i][j][0];
        if (wh[i][j][1] > rct_max)
          rct_max = wh[i][j][1];
        if (rct[i - 1][j] == '.' && rct[i][j - 1] == '.' &&
            rct[i - 1][j - 1] == '.')
          for (int w2 = wh[i][j][0], h2 = 1; h2 <= wh[i][j][1]; h2++) {
            if (wh[i - h2 + 1][j][0] < w2)
              w2 = wh[i - h2 + 1][j][0];
            if (w2 * h2 > rct_max)
              rct_max = w2 * h2;
          }
      }
    }

    cout << rct_max << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// 2011/03/18 Tazoe

#include <iostream>
using namespace std;

void recta(char rct[502][502], int y, int x, int &rct_max) {
  if (rct[y][x] == '*') {
    if (rct_max < 0)
      rct_max = 0;
    return;
  }

  int x1 = x;
  int x2 = x;
  int y1 = y;
  int y2 = y;

  while (rct[y][x1 - 1] != '*')
    x1--;
  while (rct[y][x2 + 1] != '*')
    x2++;
  while (rct[y1 - 1][x] != '*')
    y1--;
  while (rct[y2 + 1][x] != '*')
    y2++;

  if ((x2 - x1 + 1) * (y2 - y1 + 1) <= rct_max)
    return;

  for (int i1 = y1; i1 <= y; i1++) {
    for (int i2 = y2; i2 >= y; i2--) {
      if ((x2 - x1 + 1) * (i2 - i1 + 1) <= rct_max)
        continue;
      int j1;
      for (j1 = x; j1 > x1; j1--) {
        bool flg = true;
        for (int i3 = i1; i3 <= i2; i3++)
          if (rct[i3][j1 - 1] == '*') {
            flg = false;
            break;
          }
        if (!flg)
          break;
      }
      int j2;
      for (j2 = x; j2 < x2; j2++) {
        bool flg = true;
        for (int i3 = i1; i3 <= i2; i3++)
          if (rct[i3][j2 + 1] == '*') {
            flg = false;
            break;
          }
        if (!flg)
          break;
      }
      if ((j2 - j1 + 1) * (i2 - i1 + 1) > rct_max)
        rct_max = (j2 - j1 + 1) * (i2 - i1 + 1);
    }
  }
}

int main() {
  while (true) {
    int H, W;
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;

    char rct[502][502];
    for (int i = 0; i <= H + 1; i++)
      for (int j = 0; j <= W + 1; j++)
        rct[i][j] = '*';

    for (int i = 1; i <= H; i++)
      for (int j = 1; j <= W; j++)
        cin >> rct[i][j];

    int rct_max = -1;
    for (int i = 1; i <= H; i++)
      for (int j = 1; j <= W; j++)
        recta(rct, i, j, rct_max);

    cout << rct_max << endl;
  }

  return 0;
}