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

#include <iostream>
#include <vector>
std::vector<std::pair<int, int>> next_list(const int &x, const int &y) {
  return {std::make_pair(x + 1, y), std::make_pair(x - 1, y),
          std::make_pair(x, y + 1), std::make_pair(x, y - 1)};
}
template <typename T> class Queue {
public:
  Queue(const int &n) : vector(n), f(0), b(0){};
  T front() const { return vector.at(f); }
  T back() const { return vector.at(b); }
  void pop() { f = (f + 1) % vector.size(); }
  void push(T f) {
    vector.at(b) = f;
    b = (b + 1) % vector.size();
  }
  void clear() {
    f = 0;
    b = 0;
  }

private:
  std::vector<T> vector;
  int f, b;
};
struct Route {
  Route(const int &ax = 0, const int &ay = 0, const int &an = 0,
        const int &atime = 0)
      : x(ax), y(ay), n(an), time(atime){};
  int x, y, n, time;
  bool operator<(const Route &other) const { return time < other.time; }
};
int wfs(const std::vector<std::vector<int>> &vector,
        std::vector<std::vector<int>> &memo, Queue<Route> &queue,
        const Route &start, const int &goal) {
  queue.clear();
  queue.push(start);
  while (vector.at(queue.front().y).at(queue.front().x) != start.n + 1) {
    for (const auto &coord : next_list(queue.front().x, queue.front().y)) {
      if (vector.at(coord.second).at(coord.first) >= 0 &&
          memo.at(coord.second).at(coord.first) != start.n) {
        memo.at(coord.second).at(coord.first) = start.n;
        queue.push(Route(coord.first, coord.second, 0, queue.front().time + 1));
      }
    }
    queue.pop();
  }
  if (start.n + 1 == goal) {
    return queue.front().time;
  } else {
    return wfs(vector, memo, queue,
               Route(queue.front().x, queue.front().y, start.n + 1,
                     queue.front().time),
               goal);
  }
}
int main() {
  int h, w, n;
  std::cin >> h >> w >> n;
  std::vector<std::vector<int>> vector(h + 2, std::vector<int>(w + 2, -1));
  Route start(0, 0, 0, 0);
  char *arg = new char[w + 2];
  for (auto i = 1; i <= h; ++i) {
    std::cin >> arg;
    for (auto j = 1; j <= w; ++j) {
      switch (arg[j - 1]) {
      case '.':
        vector.at(i).at(j) = 0;
        break;
      case 'X':
        vector.at(i).at(j) = -1;
        break;
      case 'S':
        vector.at(i).at(j) = 0;
        start = Route(j, i, 0, 0);
        break;
      default:
        vector.at(i).at(j) = arg[j - 1] - '0';
      }
    }
  }
  delete[] arg;
  arg = nullptr;
  std::vector<std::vector<int>> memo(h + 2, std::vector<int>(w + 2, -1));
  Queue<Route> queue(h * w);
  std::cout << wfs(vector, memo, queue, start, n) << std::endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>
std::vector<std::pair<int, int>> next_list(const int &x, const int &y) {
  return {std::make_pair(x + 1, y), std::make_pair(x - 1, y),
          std::make_pair(x, y + 1), std::make_pair(x, y - 1)};
}
template <typename T> class Queue {
public:
  Queue(const int &n) : vector(n), f(0), b(0){};
  T front() const { return vector.at(f); }
  T back() const { return vector.at(b); }
  void pop() { f = (f + 1) % vector.size(); }
  void push(T f) {
    vector.at(b) = f;
    b = (b + 1) % vector.size();
  }
  void clear() {
    f = 0;
    b = 0;
  }

private:
  std::vector<T> vector;
  int f, b;
};
struct Route {
  Route(const int &ax = 0, const int &ay = 0, const int &an = 0,
        const int &atime = 0)
      : x(ax), y(ay), n(an), time(atime){};
  int x, y, n, time;
  bool operator<(const Route &other) const { return time < other.time; }
};
int wfs(const std::vector<std::vector<int>> &vector,
        std::vector<std::vector<int>> &memo, Queue<Route> &queue,
        const Route &start, const int &goal) {
  queue.clear();
  queue.push(start);
  while (vector.at(queue.front().y).at(queue.front().x) != start.n + 1) {
    for (const auto &coord : next_list(queue.front().x, queue.front().y)) {
      if (vector.at(coord.second).at(coord.first) >= 0 &&
          memo.at(coord.second).at(coord.first) != start.n) {
        memo.at(coord.second).at(coord.first) = start.n;
        queue.push(Route(coord.first, coord.second, 0, queue.front().time + 1));
      }
    }
    queue.pop();
  }
  if (start.n + 1 == goal) {
    return queue.front().time;
  } else {
    return wfs(vector, memo, queue,
               Route(queue.front().x, queue.front().y, start.n + 1,
                     queue.front().time),
               goal);
  }
}
int main() {
  int h, w, n;
  std::cin >> h >> w >> n;
  std::vector<std::vector<int>> vector(h + 2, std::vector<int>(w + 2, -1));
  Route start(0, 0, 0, 0);
  char *arg = new char[w + 2];
  for (auto i = 1; i <= h; ++i) {
    std::cin >> arg;
    for (auto j = 1; j <= w; ++j) {
      switch (arg[j - 1]) {
      case '.':
        vector.at(i).at(j) = 0;
        break;
      case 'X':
        vector.at(i).at(j) = -1;
        break;
      case 'S':
        vector.at(i).at(j) = 0;
        start = Route(j, i, 0, 0);
        break;
      default:
        vector.at(i).at(j) = arg[j - 1] - '0';
      }
    }
  }
  delete[] arg;
  arg = nullptr;
  std::vector<std::vector<int>> memo(h + 2, std::vector<int>(w + 2, -1));
  Queue<Route> queue(h * w * 4);
  std::cout << wfs(vector, memo, queue, start, n) << std::endl;
  return 0;
}