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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define pb push_back
#define INF 10000000
using namespace std;
typedef pair<int, int> P;
int w, h;
vector<string> s;
P d[10];
int antime;
P q;
map<int, int> mp;
string op;
int dp[20][20];
bool in(P n) {
  if (n.second >= 0 && n.second < w && n.first >= 0 && n.first < h)
    return true;
  return false;
}
int ans;
int ret(P b, int time) {
  // cout<<b.first<<"  "<<b.second<<endl;
  if (time > 400)
    return INF;
  if (dp[b.first][b.second] <= time) {
    q.first = b.first;
    q.second = b.second;
    return time;
  }
  P nb;
  nb.first = b.first + d[op[time % op.size()] - '0'].first;
  nb.second = b.second + d[op[time % op.size()] - '0'].second;
  if (!in(nb))
    nb = b;
  return ret(nb, time + 1);
}
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void bfs(P a) {
  queue<P> que;
  que.push(a);
  dp[a.first][a.second] = 0;
  while (!que.empty()) {
    P t = que.front(), nt;
    que.pop();
    // cout<<dp[t.first][t.second]<<endl;
    for (int i = 0; i < 4; i++) {
      nt.first = t.first + dy[i];
      nt.second = t.second + dx[i];
      if (in(nt) && dp[nt.first][nt.second] == INF &&
          s[nt.first][nt.second] != '#') {
        que.push(nt);
        dp[nt.first][nt.second] = dp[t.first][t.second] + 1;
      }
    }
  }
}
int main() {
  d[5] = P(0, 0);
  d[8] = P(-1, 0);
  d[6] = P(0, 1);
  d[4] = P(0, -1);
  d[2] = P(1, 0);
  mp[0] = 5;
  mp[1] = 8;
  mp[2] = 6;
  mp[3] = 4;
  mp[4] = 2;
  while (cin >> h >> w, h || w) {
    s.clear();
    P a, b;
    antime = INF;
    // memset(dp,-1,sizeof(dp));
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        dp[i][j] = INF;
    for (int i = 0; i < h; i++) {
      string t;
      cin >> t;
      s.pb(t);
      for (int j = 0; j < t.size(); j++) {
        if (t[j] == 'A') {
          a.first = i;
          a.second = j;
        } else if (t[j] == 'B') {
          b.first = i;
          b.second = j;
        }
      }
    }
    cin >> op;
    bfs(a);
    int ans = ret(b, 0);
    if (ans != INF)
      cout << ans << " " << q.first << " " << q.second << endl;
    else
      cout << "impossible" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define pb push_back
#define INF 10000000
using namespace std;
typedef pair<int, int> P;
int w, h;
vector<string> s;
P d[10];
int antime;
P q;
map<int, int> mp;
string op;
int dp[20][20][20][20][100];
bool in(P n) {
  if (n.second >= 0 && n.second < w && n.first >= 0 && n.first < h)
    return true;
  return false;
}
void f(int cnt) {
  cout << cnt << endl;
  f(cnt + 1);
}
int ans;
int ret(P a, P b, int time) {
  // cout<<time<<endl;

  if (a == b) {
    if (antime > time) {
      antime = time;
      q.first = a.first;
      q.second = a.second;
    }
    // cout<<a.first<<" "<<a.second<<endl;
    // cout<<time<<endl;
    return time;
  }
  if (100 < time)
    return INF;

  if (dp[a.first][a.second][b.first][b.second][time] != -1)
    return dp[a.first][a.second][b.first][b.second][time];

  int ans = INF;
  P na, nb;

  nb.first = b.first + d[op[time % op.size()] - '0'].first;
  nb.second = b.second + d[op[time % op.size()] - '0'].second;

  if (!in(nb))
    nb = b;
  for (int i = 0; i < 5; i++) {
    na.first = a.first + d[mp[i]].first;
    na.second = a.second + d[mp[i]].second;

    if (in(na) && s[na.first][na.second] != '#') {
      //<<"-------"<<endl;
      ans = min(ans, ret(na, nb, time + 1));
    } /*(time%op.size()))*/
  }
  return dp[a.first][a.second][b.first][b.second][time] = ans;
}
int main() {
  d[5] = P(0, 0);
  d[8] = P(-1, 0);
  d[6] = P(0, 1);
  d[4] = P(0, -1);
  d[2] = P(1, 0);
  mp[0] = 5;
  mp[1] = 8;
  mp[2] = 6;
  mp[3] = 4;
  mp[4] = 2;
  while (cin >> h >> w, h || w) {
    s.clear();
    P a, b;
    antime = INF;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < h; i++) {
      string t;
      cin >> t;
      s.pb(t);
      for (int j = 0; j < t.size(); j++) {
        if (t[j] == 'A') {
          a.first = i;
          a.second = j;
        } else if (t[j] == 'B') {
          b.first = i;
          b.second = j;
        }
      }
    }
    cin >> op;
    ret(a, b, 0);
    if (antime != INF)
      cout << antime << " " << q.first << " " << q.second << endl;
    else
      cout << "impossible" << endl;
    // f(0);

    // cout<<<<endl;
  }
  return 0;
}