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

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int dir[] = {0, 1, 0, -1};
int H, W, dist[25][25];
string s[25], t;
int main() {
  while (cin >> H >> W, H) {
    int ax = -1, ay = -1, bx = -1, by = -1;
    for (int i = 0; i < H; i++) {
      cin >> s[i];
      for (int j = 0; j < W; j++) {
        if (s[i][j] == 'A')
          ax = i, ay = j;
        if (s[i][j] == 'B')
          bx = i, by = j;
      }
    }
    cin >> t;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        dist[i][j] = 999999999;
      }
    }
    queue<pair<int, int>> que;
    que.push(make_pair(ax, ay));
    dist[ax][ay] = 0;
    while (!que.empty()) {
      pair<int, int> u = que.front();
      que.pop();
      for (int i = 0; i < 4; i++) {
        int tx = u.first + dir[i], ty = u.second + dir[i ^ 1];
        if (0 <= tx && tx < H && 0 <= ty && ty < W && s[tx][ty] != '#' &&
            dist[tx][ty] == 999999999) {
          dist[tx][ty] = dist[u.first][u.second] + 1;
          que.push(make_pair(tx, ty));
        }
      }
    }
    bool flag = false;
    for (int i = 0; i < 10000; i++) {
      if (dist[bx][by] <= i) {
        cout << i << ' ' << bx << ' ' << by << endl;
        flag = true;
        break;
      }
      int tx = bx, ty = by;
      if (t[i % t.size()] == '2')
        tx++;
      if (t[i % t.size()] == '4')
        ty--;
      if (t[i % t.size()] == '6')
        ty++;
      if (t[i % t.size()] == '8')
        tx--;
      if (0 <= tx && tx < H && 0 <= ty && ty < W)
        bx = tx, by = ty;
    }
    if (!flag)
      cout << "impossible" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int dir[] = {0, 1, 0, -1};
int H, W, dist[25][25];
string s[25], t;
int main() {
  while (cin >> H >> W, H) {
    int ax = -1, ay = -1, bx = -1, by = -1;
    for (int i = 0; i < H; i++) {
      cin >> s[i];
      for (int j = 0; j < W; j++) {
        if (s[i][j] == 'A')
          ax = i, ay = j;
        if (s[i][j] == 'B')
          bx = i, by = j;
      }
    }
    cin >> t;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        dist[i][j] = 999999999;
      }
    }
    queue<pair<int, int>> que;
    que.push(make_pair(ax, ay));
    dist[ax][ay] = 0;
    while (!que.empty()) {
      pair<int, int> u = que.front();
      que.pop();
      for (int i = 0; i < 4; i++) {
        int tx = u.first + dir[i], ty = u.second + dir[i ^ 1];
        if (0 <= tx && tx < H && 0 <= ty && ty < W && s[tx][ty] != '#' &&
            dist[tx][ty] == 999999999) {
          dist[tx][ty] = dist[u.first][u.second] + 1;
          que.push(make_pair(tx, ty));
        }
      }
    }
    if (dist[bx][by] == -1)
      cout << "Impossible" << endl;
    else {
      for (int i = 0;; i++) {
        if (dist[bx][by] <= i) {
          cout << i << ' ' << bx << ' ' << by << endl;
          break;
        }
        int tx = bx, ty = by;
        if (t[i % t.size()] == '2')
          tx++;
        if (t[i % t.size()] == '4')
          ty--;
        if (t[i % t.size()] == '6')
          ty++;
        if (t[i % t.size()] == '8')
          tx--;
        if (0 <= tx && tx < H && 0 <= ty && ty < W)
          bx = tx, by = ty;
      }
    }
  }
  return 0;
}