Problem p00632 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct node {
  int y, x, t, cost;
  node(int y, int x, int t, int cost) : y(y), x(x), t(t), cost(cost) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};

const int MAX = INT_MAX / 10;
typedef vector<vvi> vvvi;
int dx[] = {-1, 0, 1, 0, 0}, dy[] = {0, -1, 0, 1, 0};

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vvi field(n, vi(m));
    pii girl, ghost;
    REP(i, n) {
      string s;
      cin >> s;
      REP(j, s.size()) {
        switch (s[j]) {
        case 'A':
          girl = make_pair(i, j);
          break;
        case 'B':
          ghost = make_pair(i, j);
          break;
        case '.':
          field[i][j] = 0;
          break;
        case '#':
          field[i][j] = 1;
          break;
        default:
          assert(0);
        }
      }
    }
    string pattern;
    cin >> pattern;
    int len = pattern.size() * 20 * 20;

    vector<pii> glocs;
    REP(i, len) {
      glocs.push_back(ghost);
      switch (pattern[i % pattern.size()]) {
      case '2':
        if (ghost.first < n - 1)
          ghost.first++;
        break;
      case '4':
        if (ghost.second > 0)
          ghost.second--;
        break;
      case '8':
        if (ghost.first > 0)
          ghost.first--;
        break;
      case '6':
        if (ghost.second < m - 1)
          ghost.second++;
        break;
      case '5':
        break;
      default:
        assert(0);
      }
    }

    priority_queue<node> q;
    q.push(node(girl.first, girl.second, 0, 0));
    vvvi cost(n, vvi(m, vi(len, MAX)));
    cost[girl.first][girl.second][0] = 0;
    while (!q.empty()) {
      node cnode = q.top();
      q.pop();
      if (cost[cnode.y][cnode.x][cnode.t] < cnode.cost) {
        continue;
      }
      REP(i, 5) {
        int x = cnode.x + dx[i];
        int y = cnode.y + dy[i];
        if (x >= 0 && y >= 0 && x < m && y < n && field[y][x] == 0) {
          int newcost = cnode.cost + 1;
          int t = cnode.t + 1;
          if (t < len && cost[y][x][t] > newcost) {
            cost[y][x][t] = newcost;
            q.push(node(y, x, t, newcost));
          }
        }
      }
    }
    int ans = MAX;
    pii ansloc;
    REP(i, len) {
      int c = cost[glocs[i].first][glocs[i].second][i];
      if (c < ans) {
        ans = c;
        ansloc = glocs[i];
      }
    }
    if (ans == MAX) {
      cout << "impossible" << endl;
    } else {
      cout << ans << " " << ansloc.first << " " << ansloc.second << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct node {
  int y, x, t, cost;
  node(int y, int x, int t, int cost) : y(y), x(x), t(t), cost(cost) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};

const int MAX = INT_MAX / 10;
typedef vector<vvi> vvvi;
int dx[] = {-1, 0, 1, 0, 0}, dy[] = {0, -1, 0, 1, 0};

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vvi field(n, vi(m));
    pii girl, ghost;
    REP(i, n) {
      string s;
      cin >> s;
      REP(j, s.size()) {
        switch (s[j]) {
        case 'A':
          girl = make_pair(i, j);
          break;
        case 'B':
          ghost = make_pair(i, j);
          break;
        case '.':
          field[i][j] = 0;
          break;
        case '#':
          field[i][j] = 1;
          break;
        default:
          assert(0);
        }
      }
    }
    string pattern;
    cin >> pattern;
    int len = pattern.size() * 50;

    vector<pii> glocs;
    REP(i, len) {
      glocs.push_back(ghost);
      switch (pattern[i % pattern.size()]) {
      case '2':
        if (ghost.first < n - 1)
          ghost.first++;
        break;
      case '4':
        if (ghost.second > 0)
          ghost.second--;
        break;
      case '8':
        if (ghost.first > 0)
          ghost.first--;
        break;
      case '6':
        if (ghost.second < m - 1)
          ghost.second++;
        break;
      case '5':
        break;
      default:
        assert(0);
      }
    }

    priority_queue<node> q;
    q.push(node(girl.first, girl.second, 0, 0));
    vvvi cost(n, vvi(m, vi(len, MAX)));
    cost[girl.first][girl.second][0] = 0;
    while (!q.empty()) {
      node cnode = q.top();
      q.pop();
      if (cost[cnode.y][cnode.x][cnode.t] < cnode.cost) {
        continue;
      }
      REP(i, 5) {
        int x = cnode.x + dx[i];
        int y = cnode.y + dy[i];
        if (x >= 0 && y >= 0 && x < m && y < n && field[y][x] == 0) {
          int newcost = cnode.cost + 1;
          int t = cnode.t + 1;
          if (t < len && cost[y][x][t] > newcost) {
            cost[y][x][t] = newcost;
            q.push(node(y, x, t, newcost));
          }
        }
      }
    }
    int ans = MAX;
    pii ansloc;
    REP(i, len) {
      int c = cost[glocs[i].first][glocs[i].second][i];
      if (c < ans) {
        ans = c;
        ansloc = glocs[i];
      }
    }
    if (ans == MAX) {
      cout << "impossible" << endl;
    } else {
      cout << ans << " " << ansloc.first << " " << ansloc.second << endl;
    }
  }
}