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

#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
int H, W, sx, sy;
vector<vector<char>> M;
const vector<char> dx = {0, 1, 0, -1};
const vector<char> dy = {1, 0, -1, 0};
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0 && W == 0)
      break;
    M = vector<vector<char>>(H, vector<char>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> M[i][j];
        if (M[i][j] == '2')
          sx = j, sy = i;
      }
    }
    bool flag = false;
    queue<tuple<char, char, char, vector<vector<char>>>> que;
    que.push(make_tuple(sx, sy, 0, M));
    while (!que.empty()) {
      char x = get<0>(que.front());
      char y = get<1>(que.front());
      char t = get<2>(que.front());
      vector<vector<char>> M2 = get<3>(que.front());
      que.pop();
      if (t >= 10) {
        break;
      }
      for (int i = 0; i < 4; i++) {
        char tx = x;
        char ty = y;
        char td = 0;
        while (true) {
          if (M[ty][tx] == '3') {
            cout << (int)(t + 1) << endl;
            flag = true;
            goto Exit;
          }
          if (0 <= ty + dy[i] && ty + dy[i] < H && 0 <= tx + dx[i] &&
              tx + dx[i] < W) {
            if (M2[ty + dy[i]][tx + dx[i]] == '1') {
              if (td != 0) {
                if (t < 9) {
                  vector<vector<char>> M3 = M2;
                  M3[ty + dy[i]][tx + dx[i]] = '0';
                  que.push(make_tuple(tx, ty, t + 1, M3));
                }
              }
              break;
            }
            tx += dx[i];
            ty += dy[i];
            td++;
          } else {
            break;
          }
        }
      }
    }
  Exit:;
    if (!flag) {
      cout << -1 << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
int H, W, sx, sy;
vector<vector<char>> M;
const vector<char> dx = {0, 1, 0, -1};
const vector<char> dy = {1, 0, -1, 0};
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0 && W == 0)
      break;
    M = vector<vector<char>>(H, vector<char>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> M[i][j];
        if (M[i][j] == '2')
          sx = j, sy = i;
      }
    }
    bool flag = false;
    queue<tuple<char, char, char, vector<vector<char>>>> que;
    que.push(make_tuple(sx, sy, 0, M));
    while (!que.empty()) {
      char x = get<0>(que.front());
      char y = get<1>(que.front());
      char t = get<2>(que.front());
      if (t >= 10) {
        cout << -1 << endl;
        flag = true;
        break;
      }
      vector<vector<char>> M2 = get<3>(que.front());
      que.pop();
      for (int i = 0; i < 4; i++) {
        char tx = x;
        char ty = y;
        char td = 0;
        while (true) {
          if (M[ty][tx] == '3') {
            cout << (int)(t + 1) << endl;
            flag = true;
            goto Exit;
          }
          if (0 <= ty + dy[i] && ty + dy[i] < H && 0 <= tx + dx[i] &&
              tx + dx[i] < W) {
            if (M2[ty + dy[i]][tx + dx[i]] == '1') {
              if (td != 0) {
                vector<vector<char>> M3 = M2;
                M3[ty + dy[i]][tx + dx[i]] = '0';
                que.push(make_tuple(tx, ty, t + 1, M3));
              }
              break;
            }
            tx += dx[i];
            ty += dy[i];
            td++;
          } else {
            break;
          }
        }
      }
    }
  Exit:;
    if (!flag) {
      cout << -1 << endl;
    }
  }
  return 0;
}