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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int h, w;
  while (cin >> h >> w, h || w) {
    char ba[h][w];
    int ax, ay, bx, by;
    REP(i, h) {
      REP(j, w) {
        cin >> ba[i][j];
        if (ba[i][j] == 'A') {
          ax = j;
          ay = i;
        } else if (ba[i][j] == 'B') {
          bx = j;
          by = i;
        }
      }
    }

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

    int dis[h][w];
    REP(i, h) REP(j, w) dis[i][j] = INF;
    typedef pair<int, int> pii;
    queue<pair<int, pii>> Q;
    Q.push(make_pair(0, pii(ax, ay)));
    int maxd = 0;
    while (!Q.empty()) {
      int x = Q.front().second.first;
      int y = Q.front().second.second;
      int d = Q.front().first;
      Q.pop();

      if (dis[y][x] != INF)
        continue;
      dis[y][x] = d;
      maxd = max(maxd, d);
      REP(k, 4) {
        int xx = x + dx[k];
        int yy = y + dy[k];

        if (xx < 0 || xx >= w || yy < 0 || yy >= h)
          continue;
        if (ba[yy][xx] == '#')
          continue;
        if (dis[yy][xx] != INF)
          continue;

        Q.push(make_pair(d + 1, pii(xx, yy)));
      }
    }
    // REP(y,h) {
    //   REP(x,w) {
    //     if (dis[y][x] == INF) cout << -1 << " ";
    //     else
    //       printf("%2d ", dis[y][x]);
    //   }
    //   cout << endl;
    // }
    string s;
    cin >> s;
    int m = s.size();
    int memo[h][w][m];
    memset(memo, 0, sizeof(memo));

    int cnt = 0;
    int res = INF;
    int ansx, ansy;
    while (1) {
      //      printf("(%d,%d) ",bx,by);
      if (dis[by][bx] <= cnt) {
        if (cnt < res) {
          res = cnt;
          ansx = bx;
          ansy = by;
        }
      }
      if (cnt >= maxd) {
        if (memo[by][bx][cnt % m]) {
          break;
        }
        memo[by][bx][cnt % m] = 1;
      }

      if (s[cnt % m] - '0' != 5) {
        int dir = (s[cnt % m] - '0') / 2 - 1;
        bx += dx[dir];
        by += dy[dir];
        if (bx < 0 || bx >= w || by < 0 || by >= h) {
          bx -= dx[dir];
          by -= dy[dir];
        }
      }

      cnt++;
    }

    if (res == INF) {
      cout << "impossible" << endl;
    } else
      printf("%d %d %d\\n", res, ansy, ansx);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int h, w;
  while (cin >> h >> w, h || w) {
    char ba[h][w];
    int ax, ay, bx, by;
    REP(i, h) {
      REP(j, w) {
        cin >> ba[i][j];
        if (ba[i][j] == 'A') {
          ax = j;
          ay = i;
        } else if (ba[i][j] == 'B') {
          bx = j;
          by = i;
        }
      }
    }
    const int dx[] = {0, -1, 1, 0};
    const int dy[] = {1, 0, 0, -1};

    int dis[h][w];
    REP(i, h) REP(j, w) dis[i][j] = INF;
    typedef pair<int, int> pii;
    queue<pair<int, pii>> Q;
    Q.push(make_pair(0, pii(ax, ay)));
    int maxd = 0;
    while (!Q.empty()) {
      int x = Q.front().second.first;
      int y = Q.front().second.second;
      int d = Q.front().first;
      Q.pop();

      dis[y][x] = d;
      maxd = max(maxd, d);
      REP(k, 4) {
        int xx = x + dx[k];
        int yy = y + dy[k];

        if (xx < 0 || xx >= w || yy < 0 || yy >= h)
          continue;
        if (ba[yy][xx] == '#')
          continue;
        if (dis[yy][xx] != INF)
          continue;

        Q.push(make_pair(d + 1, pii(xx, yy)));
      }
    }
    // REP(y,h) {
    //   REP(x,w) {
    //     if (dis[y][x] == INF) cout << -1 << " ";
    //     else
    //       printf("%2d ", dis[y][x]);
    //   }
    //   cout << endl;
    // }
    string s;
    cin >> s;
    int m = s.size();
    int memo[h][w][m];
    memset(memo, 0, sizeof(memo));

    int cnt = 0;
    int res = INF;
    int ansx, ansy;
    while (1) {
      //      printf("(%d,%d) ",bx,by);
      if (dis[by][bx] <= cnt) {
        if (cnt < res) {
          res = cnt;
          ansx = bx;
          ansy = by;
        }
      }

      if (cnt >= maxd) {
        if (memo[by][bx][cnt % m]) {
          break;
        }
        memo[by][bx][cnt % m] = 1;
      }

      if (s[cnt % m] - '0' != 5) {
        int dir = (s[cnt % m] - '0') / 2 - 1;
        bx += dx[dir];
        by += dy[dir];
        if (bx < 0 || bx >= w || by < 0 || by >= h) {
          bx -= dx[dir];
          by -= dy[dir];
        }
      }

      cnt++;
    }

    if (res == INF) {
      cout << "Impossible" << endl;
    } else
      printf("%d %d %d\\n", res, ansy, ansx);
  }
}