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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#define INF (1 << 21)
using namespace std;

class Point {
public:
  int x, y, c;
  Point(int x = 0, int y = 0, int c = 0) : x(x), y(y), c(c) {}
};

int main() {
  int i, j, k, l, h, w, sum, ans, gomi[20][20], G[11][11];
  bool f[20][20], f2[20][20];
  string str;
  while (1) {
    Point u, v, g;
    cin >> w >> h;
    if (h == 0 && w == 0)
      break;
    ans = INF;
    sum = 0;

    for (i = 0; i < h; i++) {
      cin >> str;
      for (j = 0; j < w; j++) {
        gomi[i][j] = -1;
        f[i][j] = false;
        if (str[j] == 'o')
          gomi[i][j] = 0;
        else if (str[j] == 'x')
          f[i][j] = true;
        else if (str[j] == '*')
          gomi[i][j] = ++sum;
      }
    }

    for (i = 0; i < sum; i++) {
      for (j = i + 1; j <= sum; j++) {
        for (k = 0; k < h; k++) {
          for (l = 0; l < w; l++) {
            f2[k][l] = f[k][l];
            if (gomi[k][l] == i) {
              u.y = k;
              u.x = l;
              f2[k][l] = true;
            } else if (gomi[k][l] == j) {
              g.y = k;
              g.x = l;
            }
          }
        }

        queue<Point> Q;
        bool ff = true;
        u.c = 0;
        Q.push(u);
        while (!Q.empty()) {
          u = Q.front();
          Q.pop();
          if (g.x == u.x && g.y == u.y) {
            G[i][j] = G[j][i] = u.c;
            ff = false;
            break;
          }
          u.c++;
          int dx[4] = {1, 0, -1, 0};
          int dy[4] = {0, 1, 0, -1};
          for (k = 0; k < 4; k++) {
            v = u;
            v.x += dx[k];
            v.y += dy[k];
            if (v.x >= 0 && v.x < w && v.y >= 0 && v.y < h && !f2[v.y][v.x]) {
              f2[v.y][v.x] = true;
              Q.push(v);
            }
          }
        }
        if (ff) {
          ans = -1;
          break;
        }
      }
      if (ans == -1)
        break;
    }

    if (ans != -1) {
      int a[11], be, af, A;
      for (i = 0; i <= sum; i++)
        a[i] = i;
      do {
        if (a[0] == 0) {
          A = 0;
          be = a[0];
          for (i = 1; i <= sum; i++) {
            af = a[i];
            A += G[be][af];
            be = af;
          }
          ans = min(ans, A);
        }
      } while (next_permutation(a, a + sum + 1));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

class Point {
public:
  int x, y, g, c;
};

int main() {
  int i, j, h, w, sum, ans, now;
  bool gomi[20][20], f[11][20][20];
  string str;
  while (1) {
    Point u, v;
    queue<Point> Q;
    cin >> w >> h;
    if (h == 0 && w == 0)
      break;
    ans = -1;
    sum = 0;
    for (i = 0; i < h; i++) {
      cin >> str;
      for (j = 0; j < w; j++) {
        gomi[i][j] = false;
        for (int k = 0; k < 11; k++)
          f[k][i][j] = false;
        if (str[j] == 'o') {
          u.y = i;
          u.x = j;
        } else if (str[j] == 'x') {
          for (int k = 0; k < 11; k++)
            f[k][i][j] = true;
        } else if (str[j] == '*') {
          gomi[i][j] = true;
          sum++;
        }
      }
    }

    now = 0;
    u.g = 0;
    u.c = 0;
    Q.push(u);
    while (!Q.empty()) {
      u = Q.front();
      Q.pop();
      if (u.g < now)
        continue;

      f[u.g][u.y][u.x] = true;

      if (gomi[u.y][u.x]) {
        gomi[u.y][u.x] = false;
        u.g++;
        now++;
      }

      if (u.g == sum) {
        ans = u.c;
        break;
      }

      u.c++;
      f[u.g][u.y][u.x] = true;
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      for (i = 0; i < 4; i++) {
        v = u;
        v.x += dx[i];
        v.y += dy[i];
        if (!f[v.g][v.y][v.x] && v.x >= 0 && v.x < w && v.y >= 0 && v.y < h) {
          Q.push(v);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}