Problem p01103 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[15][15];
int fx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int fy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
bool vis[15][15];
queue<pair<int, int>> q;
set<int> seto;
int bcj[110];
int calc(int x, int y) { return 10 * (x - 1) + y; }
int father(int x) { return bcj[x] == x ? x : bcj[x] = father(bcj[x]); }
void join(int x, int y) {
  int fx = father(x);
  int fy = father(y);
  if (fx != fy)
    bcj[fy] = fx;
}
int main() {
  int d, w;
  while (1) {
    scanf("%d%d", &d, &w);
    if (d == 0 && w == 0)
      break;
    for (int i = 1; i <= d; i++)
      for (int j = 1; j <= w; j++)
        scanf("%d", &a[i][j]);
    int ans = 0;
    for (int i = 2; i <= d - 1; i++)
      for (int j = 2; j <= w - 1; j++) {
        int val = a[i][j];
        memset(vis, false, sizeof(vis));
        vis[i][j] = true;
        while (!q.empty())
          q.pop();
        q.push(make_pair(i, j));
        bool flag = true;
        while (!q.empty()) {
          int x = q.front().first;
          int y = q.front().second;
          q.pop();
          if (x == 1 || x == d || y == 1 || y == w)
            flag = false;
          for (int ii = 0; ii < 4; ii++) {
            int xx = x + fx[ii];
            int yy = y + fy[ii];
            if (xx >= 1 && xx <= d && yy >= 1 && yy <= w && !vis[xx][yy] &&
                a[xx][yy] <= val) {
              vis[xx][yy] = true;
              q.push(make_pair(xx, yy));
            }
          }
        }
        if (!flag)
          continue;
        for (int ii = 1; ii <= 100; ii++)
          bcj[ii] = ii;
        for (int ii = 1; ii <= d; ii++)
          for (int jj = 1; jj <= w; jj++) {
            if (!vis[ii][jj]) {
              for (int kk = 0; kk < 4; kk++) {
                int xx = ii + fx[kk];
                int yy = jj + fy[kk];
                if (xx >= 1 && xx <= d && yy >= 1 && yy <= w && !vis[xx][yy]) {
                  join(calc(ii, jj), calc(xx, yy));
                }
              }
            }
          }
        seto.clear();
        int sum = 0;
        int cnt = 0;
        for (int ii = 1; ii <= d; ii++)
          for (int jj = 1; jj <= w; jj++) {
            if (!vis[ii][jj]) {
              int fa = father(calc(ii, jj));
              seto.insert(fa);
            } else {
              cnt++;
              sum += a[ii][jj];
            }
          }
        if (seto.size() != 1)
          continue;
        int minx = 10086;
        int aa, bb, cc, dd;
        aa = cc = 10086;
        bb = dd = -10086;
        for (int ii = 1; ii <= d; ii++)
          for (int jj = 1; jj <= w; jj++) {
            if (vis[ii][jj]) {
              aa = min(aa, ii);
              bb = max(bb, ii);
              cc = min(cc, jj);
              dd = max(dd, jj);
              for (int kk = 0; kk < 8; kk++) {
                int xx = ii + fx[kk];
                int yy = jj + fy[kk];
                if (xx >= 1 && xx <= d && yy >= 1 && yy <= w && !vis[xx][yy]) {
                  minx = min(minx, a[xx][yy]);
                }
              }
            }
          }
        flag = true;
        for (int ii = aa; ii <= bb; ii++)
          for (int jj = cc; jj <= dd; jj++)
            if (!vis[ii][jj])
              flag = false;
        if (minx > val && flag) {
          int res = cnt * minx - sum;
          ans = max(ans, res);
        }
      }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define test(a) printf("(%3d)", a)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int MAXN = 5e4 + 10;

int vis[11][11][11][11];
struct Node {
  int x, y, xx, yy;
  Node() {}
  Node(int x, int y, int xx, int yy) : x(x), y(y), xx(xx), yy(yy) {}
} node;
queue<Node> q;

int n, m, a[11][11];
int ans;

int main(void) {
  while (scanf("%d%d", &n, &m) && (n || m)) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &a[i][j]);
      }
    }

    ans = 0;
    memset(vis, 0, sizeof(vis));

    vis[2][2][n - 1][m - 1] = 1;
    q.push(Node(2, 2, n - 1, m - 1));
    while (!q.empty()) {
      node = q.front();
      q.pop();
      if (node.x > node.xx || node.y > node.yy)
        continue;
      vis[node.x][node.y][node.xx][node.yy] = 1;

      int low = 10, up = 0, tmp = 0;
      for (int i = node.x - 1; i <= node.xx + 1; i++) {
        low = min(low, a[i][node.yy + 1]);
        low = min(low, a[i][node.y - 1]);
      }
      for (int j = node.y; j <= node.yy; j++) {
        low = min(low, a[node.x - 1][j]);
        low = min(low, a[node.xx + 1][j]);
      }
      for (int i = node.x; i <= node.xx; i++) {
        for (int j = node.y; j <= node.yy; j++) {
          up = max(a[i][j], up);
          tmp += low - a[i][j];
        }
      }
      if (up < low)
        ans = max(tmp, ans);
      else {
        if (!vis[node.x + 1][node.y][node.xx][node.yy])
          q.push(Node(node.x + 1, node.y, node.xx, node.yy));
        if (!vis[node.x][node.y + 1][node.xx][node.yy])
          q.push(Node(node.x, node.y + 1, node.xx, node.yy));
        if (!vis[node.x][node.y][node.xx - 1][node.yy])
          q.push(Node(node.x, node.y, node.xx - 1, node.yy));
        if (!vis[node.x][node.y][node.xx][node.yy - 1])
          q.push(Node(node.x, node.y, node.xx, node.yy - 1));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}