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

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P2;

int v[30][30];
bool vb[30][30][20];
string g[30], L;
int h, w, s;
int ay, ax, by, bx;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};

int main() {
  while (cin >> h >> w, h || w) {
    for (int i = 0; i < h; i++)
      cin >> g[i];
    cin >> L;
    s = L.size();

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (g[i][j] == 'A') {
          ay = i;
          ax = j;
        }
        if (g[i][j] == 'B') {
          by = i;
          bx = j;
        }
        v[i][j] = -1;
        for (int k = 0; k < s; k++)
          vb[i][j][k] = false;
      }

    queue<P2> q;
    v[ay][ax] = 0;
    q.push(P2(0, P(ay, ax)));

    while (!q.empty()) {
      P2 p = q.front();
      q.pop();

      for (int i = 0; i < 4; i++) {
        int sy = p.second.first + dy[i], sx = p.second.second + dx[i];
        if (sy < 0 || sx < 0 || h <= sy || w <= sx)
          continue;
        if (v[sy][sx] >= 0 || g[sy][sx] == '#')
          continue;
        v[sy][sx] = p.first + 1;
        q.push(P2(p.first + 1, P(sy, sx)));
      }
    }

    int t = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        t = max(t, v[i][j]);

    for (int i = 0;; i++) {
      if (i <= t) {
        if (v[by][bx] >= 0 && v[by][bx] <= i) {
          cout << i << " " << by << " " << bx << endl;
          break;
        }
      } else {
        if (v[by][bx] >= 0) {
          cout << i << " " << by << " " << bx << endl;
          break;
        }
        if (vb[by][bx][i % s]) {
          cout << "impossible\\n";
          break;
        }
      }
      vb[by][bx][i % s] = true;
      if (L[i % s] == '8' && 0 < by)
        by--;
      if (L[i % s] == '6' && bx < w - 1)
        bx++;
      if (L[i % s] == '2' && by < h - 1)
        by++;
      if (L[i % s] == '4' && 0 < bx)
        bx--;
    }
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P2;

bool v[30][30], vb[30][30][20];
string g[30], L;
int h, w, s;
int ay, ax, by, bx;

int main() {
  while (cin >> h >> w, h || w) {
    for (int i = 0; i < h; i++)
      cin >> g[i];
    cin >> L;
    s = L.size();
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (g[i][j] == 'A') {
          g[i][j] == '.';
          ay = i;
          ax = j;
        }
        if (g[i][j] == 'B') {
          g[i][j] == '.';
          by = i;
          bx = j;
        }
        v[i][j] = false;
        for (int k = 0; k < s; k++)
          vb[i][j][k] = false;
      }

    int t = 0;
    queue<P2> q;
    q.push(P2(0, P(ay, ax)));

    while (!q.empty()) {
      P2 p = q.front();
      q.pop();
      /*
      cout << "[" << p.first << " " << p.second.first << " " << p.second.second
      << "]" << endl; cout << by << " " << bx << endl;
      */
      if (p.first == t + 1) {
        vb[by][bx][t % s] = true;
        if (v[by][bx])
          break;
        if (L[t % s] == '8' && 0 < by)
          by--;
        if (L[t % s] == '6' && bx < w - 1)
          bx++;
        if (L[t % s] == '2' && by < h - 1)
          by++;
        if (L[t % s] == '4' && 0 < bx)
          bx--;
        t++;
      }

      v[p.second.first][p.second.second] = true;

      int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        int sy = p.second.first + dy[i], sx = p.second.second + dx[i];
        if (sy < 0 || sx < 0 || h <= sy || w <= sx)
          continue;
        if (v[sy][sx] || g[sy][sx] == '#')
          continue;
        q.push(P2(p.first + 1, P(sy, sx)));
      }
    }

    if (v[by][bx])
      cout << t << " " << by << " " << bx << endl;
    else {
      for (int i = 0;; i++) {
        if (vb[by][bx][(t + i) % s]) {
          // cout << t+i << endl;
          cout << "impossible\\n";
          break;
        }
        vb[by][bx][(t + i) % s] = true;
        if (L[(t + i) % s] == '2' && 0 < by)
          by--;
        if (L[(t + i) % s] == '6' && bx < w - 1)
          bx++;
        if (L[(t + i) % s] == '8' && by < h - 1)
          by++;
        if (L[(t + i) % s] == '4' && 0 < bx)
          bx--;
        if (v[by][bx]) {
          cout << t + i + 1 << " " << by << " " << bx << endl;
          break;
        }
      }
    }
  }
}