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

#include <bits/stdc++.h>
using namespace std;

typedef vector<pair<char, char>> VPII;
typedef pair<char, pair<pair<char, char>, VPII>> PIPPIIVPII;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void solve(int W, int H) {
  VPII M;
  pair<char, char> start, goal;
  for (size_t i = 1; i <= H; i++) {
    for (size_t j = 1; j <= W; j++) {
      int x;
      cin >> x;
      if (x == 1) {
        M.push_back(make_pair(i, j));
      } else if (x == 2) {
        start = make_pair(i, j);
      } else if (x == 3) {
        goal = make_pair(i, j);
      }
    }
  }
  queue<PIPPIIVPII> que;
  que.push(make_pair(0, make_pair(start, M)));
  while (que.size()) {
    PIPPIIVPII now = que.front();
    que.pop();
    pair<char, char> nowplace = now.second.first;
    if (now.first < 10) {
      for (size_t i = 0; i < 4; i++) {
        auto nextplace = nowplace;
        auto nextP = now.second.second;
        bool f = false;
        while (true) {
          nextplace.first += dx[i];
          nextplace.second += dy[i];
          if (nextplace.first == 0 || nextplace.first == H + 1 ||
              nextplace.second == 0 || nextplace.second == W + 1) {
            f = true;
            break;
          }
          if (nextplace == goal) {
            cout << (int)now.first + 1 << endl;
            return;
          }
          bool g = false;
          for (auto ite = nextP.begin(); ite < nextP.end(); ite++) {
            if (*ite == nextplace) {
              nextplace.first -= dx[i];
              nextplace.second -= dy[i];
              if (nowplace != nextplace) {
                nextP.erase(ite);
                que.push(make_pair(now.first + 1, make_pair(nextplace, nextP)));
              }
              g = true;
              break;
            }
          }
          if (g) {
            break;
          }
        }
      }
    } else {
      break;
    }
  }
  cout << -1 << endl;
  return;
}

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    solve(W, H);
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef vector<pair<int, int>> VPII;
typedef pair<int, pair<pair<int, int>, VPII>> PIPPIIVPII;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void solve(int W, int H) {
  VPII M;
  pair<int, int> start, goal;
  for (size_t i = 1; i <= H; i++) {
    for (size_t j = 1; j <= W; j++) {
      int x;
      cin >> x;
      if (x == 1) {
        M.push_back(make_pair(i, j));
      } else if (x == 2) {
        start = make_pair(i, j);
      } else if (x == 3) {
        goal = make_pair(i, j);
      }
    }
  }
  queue<PIPPIIVPII> que;
  que.push(make_pair(0, make_pair(start, M)));
  while (que.size()) {
    PIPPIIVPII now = que.front();
    que.pop();
    pair<int, int> nowplace = now.second.first;
    if (now.first >= 10) {
      cout << -1 << endl;
      return;
    }
    for (size_t i = 0; i < 4; i++) {
      auto nextplace = nowplace;
      auto nextP = now.second.second;
      bool f = false;
      while (true) {
        nextplace.first += dx[i];
        nextplace.second += dy[i];
        if (nextplace.first == 0 || nextplace.first == H + 1 ||
            nextplace.second == 0 || nextplace.second == W + 1) {
          f = true;
          break;
        }
        if (nextplace == goal) {
          cout << now.first + 1 << endl;
          return;
        }
        for (auto ite = nextP.begin(); ite < nextP.end(); ite++) {
          if (*ite == nextplace) {
            nextplace.first -= dx[i];
            nextplace.second -= dy[i];
            if (nowplace != nextplace) {
              nextP.erase(ite);
              if (now.first < 10) {
                que.push(make_pair(now.first + 1, make_pair(nextplace, nextP)));
              }
            }
            f = true;
            break;
          }
        }
        if (f) {
          break;
        }
      }
      if (f) {
        continue;
      }
    }
  }
  cout << -1 << endl;
  return;
}

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    solve(W, H);
  }
}