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

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

#define rep(i, n) for (int i = 0; i < n; i++)

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

class NODE {
public:
  int x1;
  int y1;
  int x2;
  int y2;
  int depth;
};

int conv(char c) {
  switch (c) {
  case '2':
    return 0;
  case '4':
    return 2;
  case '8':
    return 1;
  case '6':
    return 3;
  case '5':
    return 4;
  }
  cout << "aaaaaaaa" << endl;
}
int main() {
  int X, Y, tx, ty, kx, ky;
  char data[22][22];
  string cmd;
  while (cin >> Y >> X, X) {
    rep(i, 22) rep(j, 22) data[i][j] = '+';
    rep(i, Y) rep(j, X) {
      cin >> data[1 + i][1 + j];
      if (data[1 + i][1 + j] == 'A') {
        tx = 1 + j;
        ty = 1 + i;
        data[1 + i][1 + j] = '.';
      }
      if (data[1 + i][1 + j] == 'B') {
        kx = 1 + j;
        ky = 1 + i;
        data[1 + i][1 + j] = '.';
      }
    }
    cin >> cmd;
    queue<NODE> Q;
    NODE t = {tx, ty, kx, ky, 0};
    Q.push(t);

    bool done[22][22][22][22][10] = {0};

    while (Q.size()) {
      NODE q = Q.front();
      Q.pop();
      if (done[q.x1][q.y1][q.x2][q.y2][q.depth % cmd.size()])
        continue;
      done[q.x1][q.y1][q.x2][q.y2][q.depth % cmd.size()] = true;

      if (q.x1 == q.x2 && q.y1 == q.y2) {
        cout << q.depth << " " << q.y1 - 1 << " " << q.x1 - 1 << endl;
        goto end;
      }
      rep(i, 5) {
        NODE next;
        next.x1 = q.x1 + dx[i];
        next.y1 = q.y1 + dy[i];
        next.x2 = q.x2 + dx[conv(cmd[q.depth % cmd.size()])];
        next.y2 = q.y2 + dy[conv(cmd[q.depth % cmd.size()])];
        next.depth = q.depth + 1;
        if (data[next.y1][next.x1] != '.') {
          next.x1 = q.x1;
          next.y1 = q.y1;
        }
        if (data[next.y2][next.x2] == '+') {
          next.x2 = q.x2;
          next.y2 = q.y2;
        }
        Q.push(next);
      }
    }
    cout << "impossible" << endl;
  end:;
  }
}

Error: TLE

Fixed submission:

/* TLE */

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define PTN (dp % L.size())
int w, h;
char data[20][20];
string L;
bool flag = false;
int mi, mix, miy;
pair<int, int> prob[1000];
bool done[20][20];

void rec(int x, int y, int gx, int gy, int bx, int by, int dp) {
  if (data[y][x] == '#' || dp > mi)
    return;
  if (x == gx && y == gy) {
    flag = true;
    if (mi > dp) {
      mi = dp;
      mix = x;
      miy = y;
    }
    return;
  }

  switch (L[PTN]) {
  case '5':
    // rec(x,y,gx,gy,x,y,dp+1);
    break;
  case '8':
    if (gy - 1 >= 0)
      gy -= 1;
    break;
  case '6':
    if (gx + 1 < w)
      gx += 1;
    break;
  case '4':
    if (gx - 1 >= 0)
      gx -= 1;
    break;
  case '2':
    if (gy + 1 < h)
      gy += 1;
    break;
  }
  if (!done[y][x])
    rec(x, y, gx, gy, x, y, dp + 1);
  done[y][x] = true;

  if (x - 1 != bx && x - 1 >= 0) {
    rec(x - 1, y, gx, gy, x, y, dp + 1);
  }
  if (x + 1 != bx && x + 1 < w) {
    rec(x + 1, y, gx, gy, x, y, dp + 1);
  }
  if (y + 1 != by && y + 1 < h) {
    rec(x, y + 1, gx, gy, x, y, dp + 1);
  }
  if (y - 1 != by && y - 1 >= 0) {
    rec(x, y - 1, gx, gy, x, y, dp + 1);
  }
}
int main() {
  int sx, sy;
  int gx, gy;
  while (cin >> h >> w && w || h) {
    flag = false;
    rep(i, 20) rep(j, 20) done[i][j] = false;
    mi = 100;
    rep(i, h) rep(j, w) {
      cin >> data[i][j];
      switch (data[i][j]) {
      case 'A':
        sx = j;
        sy = i;
        break;
      case 'B':
        gx = j;
        gy = i;
        break;
      }
    }
    cin >> L;

    rec(sx, sy, gx, gy, sx, sy, 0);

    if (flag) {
      cout << mi << " " << miy << " " << mix << endl;
    } else {
      cout << "impossible" << endl;
    }
  }
}