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

// 35
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>

using namespace std;

char g[20][20];
int h, w;
int t[20][20];

struct S {
  int t, x, y;
};

int main() {
  while (cin >> h >> w, h | w) {
    int ax, ay, bx, by;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> g[y][x];
        if (g[y][x] == 'A') {
          ax = x;
          ay = y;
        } else if (g[y][x] == 'B') {
          bx = x;
          by = y;
        }
      }
    }
    fill(t[0], t[20], 1 << 30);
    queue<S> que;
    S is = {0, ax, ay};
    que.push(is);
    while (!que.empty()) {
      int x = que.front().x;
      int y = que.front().y;
      int ct = que.front().t;
      que.pop();
      if (t[y][x] <= ct)
        continue;
      t[y][x] = ct;
      for (int i = 0; i < 4; i++) {
        int d[] = {0, 1, 0, -1, 0};
        int nx = x + d[i];
        int ny = y + d[i + 1];
        if (nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] <= ct + 1 ||
            g[ny][nx] == '#')
          continue;
        S ns = {ct + 1, nx, ny};
        que.push(ns);
      }
    }
    string p;
    cin >> p;
    int e;
    for (e = 0; e < 10000; e++) {
      if (t[by][bx] <= e)
        break;
      char o = p[e % p.size()];
      int ny = by + (o == '2') - (o == '8');
      int nx = bx + (o == '6') - (o == '4');
      if (0 <= ny && ny < h && 0 <= nx && nx < w) {
        by = ny;
        bx = nx;
      }
    }
    if (e < 10000) {
      cout << e << ' ' << by << ' ' << bx << endl;
    } else {
      cout << "impossible" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 35
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>

using namespace std;

char g[20][20];
int h, w;
int t[20][20];

struct S {
  int t, x, y;
};

int main() {
  while (cin >> h >> w, h | w) {
    int ax, ay, bx, by;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> g[y][x];
        if (g[y][x] == 'A') {
          ax = x;
          ay = y;
        } else if (g[y][x] == 'B') {
          bx = x;
          by = y;
        }
      }
    }
    fill(t[0], t[20], 1 << 30);
    queue<S> que;
    S is = {0, ax, ay};
    que.push(is);
    while (!que.empty()) {
      int x = que.front().x;
      int y = que.front().y;
      int ct = que.front().t;
      que.pop();
      t[y][x] = ct;
      for (int i = 0; i < 4; i++) {
        int d[] = {0, 1, 0, -1, 0};
        int nx = x + d[i];
        int ny = y + d[i + 1];
        if (nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] <= ct + 1 ||
            g[ny][nx] == '#')
          continue;
        S ns = {ct + 1, nx, ny};
        que.push(ns);
      }
    }
    string p;
    cin >> p;
    int e;
    for (e = 0; e < 10000; e++) {
      if (t[by][bx] <= e)
        break;
      char o = p[e % p.size()];
      int ny = by + (o == '2') - (o == '8');
      int nx = bx + (o == '6') - (o == '4');
      if (0 <= ny && ny < h && 0 <= nx && nx < w) {
        by = ny;
        bx = nx;
      }
    }
    if (e < 10000) {
      cout << e << ' ' << by << ' ' << bx << endl;
    } else {
      cout << "impossible" << endl;
    }
  }
  return 0;
}