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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<vector<int>> Array2;
typedef vector<LL> LArray;
typedef vector<P> PArray;
typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() {
  int itiv;
  return (cin >> itiv, itiv);
}
template <typename T> void readAll(vector<T> &vec) {
  for (int i = 0, size = vec.size(); i < size; i++)
    cin >> vec[i];
}
template <typename T, typename U> void readAll(vector<pair<T, U>> &vec) {
  for (int i = 0, size = vec.size(); i < size; i++)
    cin >> vec[i].first >> vec[i].second;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

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

int main(void) {
  ios::sync_with_stdio(false);
  int h, w, n;
  while (cin >> h >> w >> n && n) {
    SArray stage(h);
    PArray cheese(n + 1);
    for (int i = 0; i < h; i++) {
      cin >> stage[i];
      for (int j = 0; j < w; j++) {
        char c = stage[i][j];
        if (c == 'S') {
          cheese[0] = P(i, j);
        }
        if (inRange(10, c - '0')) {
          cheese[c - '0'] = P(i, j);
        }
      }
    }

    int ans = 0;
    vector<vector<bool>> visited(h, vector<bool>(w));
    for (int i = 0; i < n; i++) {
      P start = cheese[i];
      P goal = cheese[i + 1];

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          visited[i][j] = false;
        }
      }

      queue<IP> que;
      que.push(IP(0, start));
      while (!que.empty()) {
        IP ip = que.front();
        que.pop();
        P p = ip.second;
        visited[p.first][p.second] = true;

        if (p == goal) {
          ans += ip.first;
          break;
        }

        for (int j = 0; j < 4; j++) {
          int nr = p.first + dr[j], nc = p.second + dc[j];
          if (!inRange(h, nr) || !inRange(w, nc) || stage[nr][nc] == 'X' ||
              visited[nr][nc])
            continue;
          visited[nr][nc] = true;
          que.push(IP(ip.first + 1, P(nr, nc)));
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<vector<int>> Array2;
typedef vector<LL> LArray;
typedef vector<P> PArray;
typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() {
  int itiv;
  return (cin >> itiv, itiv);
}
template <typename T> void readAll(vector<T> &vec) {
  for (int i = 0, size = vec.size(); i < size; i++)
    cin >> vec[i];
}
template <typename T, typename U> void readAll(vector<pair<T, U>> &vec) {
  for (int i = 0, size = vec.size(); i < size; i++)
    cin >> vec[i].first >> vec[i].second;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

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

int main(void) {
  ios::sync_with_stdio(false);
  int h, w, n;
  while (cin >> h >> w >> n && n) {
    SArray stage(h);
    PArray cheese(n + 1);
    for (int i = 0; i < h; i++) {
      cin >> stage[i];
      for (int j = 0; j < w; j++) {
        char c = stage[i][j];
        if (c == 'S') {
          cheese[0] = P(i, j);
        }
        if (inRange(10, c - '0')) {
          cheese[c - '0'] = P(i, j);
        }
      }
    }

    int ans = 0;
    vector<vector<bool>> visited(h, vector<bool>(w));
    for (int i = 0; i < n; i++) {
      P start = cheese[i];
      P goal = cheese[i + 1];

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          visited[i][j] = false;
        }
      }
      queue<IP> que;
      que.push(IP(0, start));
      while (!que.empty()) {
        IP ip = que.front();
        que.pop();
        P p = ip.second;
        visited[p.first][p.second] = true;

        if (p == goal) {
          ans += ip.first;
          break;
        }

        for (int j = 0; j < 4; j++) {
          int nr = p.first + dr[j], nc = p.second + dc[j];
          if (!inRange(h, nr) || !inRange(w, nc) || stage[nr][nc] == 'X' ||
              visited[nr][nc])
            continue;
          visited[p.first][p.second] = true;
          que.push(IP(ip.first + 1, P(nr, nc)));
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}