Problem p04009 (C++)

Buggy submission (status: Time Limit Exceeded):
// 修改自提交记录：232713
// 原记录作者：E.Space (UID: 1579)
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
char map[111][111], tmp[111];
int sr[111][111];
int sc[111][111];
int f[111][111];
int main() {
  freopen("robot.in", "r", stdin);
  freopen("robot.out", "w", stdout);
  int n = io::F(), m = io::F();
  for (register int i = 1; i <= n; ++i)
    scanf("%s", map[i] + 1);
  int x, y;
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= m; ++j)
      if (map[i][j] == 'E') {
        x = i, y = j;
        break;
      }
  if (x > n / 2) {
    x = n + 1 - x;
    for (register int i = 1, j = n; i < j; ++i, --j)
      memcpy(tmp, map[i], sizeof(tmp)), memcpy(map[i], map[j], sizeof(tmp)),
          memcpy(map[j], tmp, sizeof(tmp));
  }
  if (y > m / 2) {
    y = m + 1 - y;
    for (register int i = 1; i <= n; ++i)
      std::reverse(map[i] + 1, map[i] + m + 1);
  }
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= m; ++j)
      sr[i][j] = sr[i][j - 1] + (map[i][j] == 'o'),
      sc[i][j] = sc[i - 1][j] + (map[i][j] == 'o');
  int ans = 0;
  for (register int i = 1; i <= x; ++i)
    for (register int j = 1; j <= y; ++j) {
      int d = i + n - x - x + 1, r = j + m - y - y + 1;
      for (register int k = i; k <= d; ++k)
        for (register int l = j; l <= r; ++l)
          f[k][l] = 0;
      for (register int k = i; k < i + x; ++k)
        f[i][j] += sr[k][j + y - 1] - sr[k][j - 1];
      for (register int k = i; k <= d; ++k)
        for (register int l = j; l <= r; ++l) {
          int t = f[k][l] + sc[k + x - 1][l + y] - sc[k - 1][l + y];
          cmax(f[k][l + 1], t);
          t = f[k][l] + sr[k + x][l + y - 1] - sr[k + x][l - 1];
          cmax(f[k + 1][l], t);
        }
      cmax(ans, f[d][r]);
    }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
// 修改自提交记录：232713
// 原记录作者：E.Space (UID: 1579)
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
char map[111][111], tmp[111];
int sr[111][111];
int sc[111][111];
int f[111][111];
int main() {
  int n = io::F(), m = io::F();
  for (register int i = 1; i <= n; ++i)
    scanf("%s", map[i] + 1);
  int x, y;
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= m; ++j)
      if (map[i][j] == 'E') {
        x = i, y = j;
        break;
      }
  if (x > n / 2) {
    x = n + 1 - x;
    for (register int i = 1, j = n; i < j; ++i, --j)
      memcpy(tmp, map[i], sizeof(tmp)), memcpy(map[i], map[j], sizeof(tmp)),
          memcpy(map[j], tmp, sizeof(tmp));
  }
  if (y > m / 2) {
    y = m + 1 - y;
    for (register int i = 1; i <= n; ++i)
      std::reverse(map[i] + 1, map[i] + m + 1);
  }
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= m; ++j)
      sr[i][j] = sr[i][j - 1] + (map[i][j] == 'o'),
      sc[i][j] = sc[i - 1][j] + (map[i][j] == 'o');
  int ans = 0;
  for (register int i = 1; i <= x; ++i)
    for (register int j = 1; j <= y; ++j) {
      int d = i + n - x - x + 1, r = j + m - y - y + 1;
      for (register int k = i; k <= d; ++k)
        for (register int l = j; l <= r; ++l)
          f[k][l] = 0;
      for (register int k = i; k < i + x; ++k)
        f[i][j] += sr[k][j + y - 1] - sr[k][j - 1];
      for (register int k = i; k <= d; ++k)
        for (register int l = j; l <= r; ++l) {
          int t = f[k][l] + sc[k + x - 1][l + y] - sc[k - 1][l + y];
          cmax(f[k][l + 1], t);
          t = f[k][l] + sr[k + x][l + y - 1] - sr[k + x][l - 1];
          cmax(f[k + 1][l], t);
        }
      cmax(ans, f[d][r]);
    }
  printf("%d\n", ans);
  return 0;
}