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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define LDcout(x, n) fixed << setprecision(n) << x

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF = 1 << 24;
int h, w;
int dd[] = {0, 1, 0, -1, 0};
bool check(int a, int b) {
  if (0 <= a && a < h && 0 <= b && b < w)
    return true;
  return false;
}
void bfs(pii p, int x, vector<string> &v, vvint &vv) {
  if (vv[p.F][p.S] <= x)
    return;
  vv[p.F][p.S] = x;
  rep(i, 4) {
    pii ne = pii(p.F + dd[i], p.S + dd[i + 1]);
    if (!check(ne.F, ne.S) || v[ne.F][ne.S] == '#')
      continue;
    bfs(ne, x + 1, v, vv);
  }
}
void mainmain() {
  vector<pii> vec(10);
  vec[2] = pii(1, 0);
  vec[4] = pii(0, -1);
  vec[5] = pii(0, 0);
  vec[6] = pii(0, 1);
  vec[8] = pii(-1, 0);
  while (cin >> h >> w, h || w) {
    vector<string> v(h);
    rep(i, h) cin >> v[i];
    string s;
    cin >> s;
    vvint vv;
    initvv(vv, h, w, -1);
    pii A, B;
    rep(i, h) {
      rep(j, w) {
        if (v[i][j] == 'A') {
          A = pii(i, j);
          v[i][j] = '.';
        }
        if (v[i][j] == 'B') {
          B = pii(i, j);
          v[i][j] = '.';
        }
      }
    }
    // bfs(A,0,v,vv);
    queue<pair<pii, int>> q;
    q.push(mkp(A, 0));
    vv[A.F][A.S] = 0;
    while (q.size()) {
      auto top = q.front();
      q.pop();
      rep(i, 4) {
        pii ne(top.F.F + dd[i], top.F.S + dd[i + 1]);
        if (!check(ne.F, ne.S) || v[ne.F][ne.S] == '#' || vv[ne.F][ne.S] >= 0)
          continue;
        vv[ne.F][ne.S] = top.S + 1;
        q.push(mkp(ne, top.S + 1));
      }
    }
    int ans = -1;
    pii pos;
    rep(i, h) {
      rep(j, w) {
        // printf("%2d ",vv[i][j]);
        if (vv[i][j] == -1)
          vv[i][j] = INF;
      }
      // cout<<endl;
    }
    rep(i, 1e5) {
      // cout<<i<<" "<<B.F<<" "<<B.S<<endl;
      if (vv[B.F][B.S] <= i) {
        ans = i;
        pos = B;
        break;
      }
      int t = s[i % s.size()] - '0';
      pii ne = pii(B.F + vec[t].F, B.S + vec[t].S);
      if (check(ne.F, ne.S)) {
        B = ne;
      }
    }
    if (ans == -1) {
      cout << "impossible" << endl;
    } else {
      cout << ans << " " << pos.F << " " << pos.S << endl;
    }
    // return;
  }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  mainmain();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define LDcout(x, n) fixed << setprecision(n) << x

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF = 1 << 24;
int h, w;
int dd[] = {0, 1, 0, -1, 0};
bool check(int a, int b) {
  if (0 <= a && a < h && 0 <= b && b < w)
    return true;
  return false;
}
void bfs(pii p, int x, vector<string> &v, vvint &vv) {
  if (vv[p.F][p.S] >= 0)
    return;
  vv[p.F][p.S] = x;
  rep(i, 4) {
    pii ne = pii(p.F + dd[i], p.S + dd[i + 1]);
    if (!check(ne.F, ne.S) || v[ne.F][ne.S] == '#')
      continue;
    bfs(ne, x + 1, v, vv);
  }
}
void mainmain() {
  vector<pii> vec(10);
  vec[2] = pii(1, 0);
  vec[4] = pii(0, -1);
  vec[5] = pii(0, 0);
  vec[6] = pii(0, 1);
  vec[8] = pii(-1, 0);
  while (cin >> h >> w, h || w) {
    vector<string> v(h);
    rep(i, h) cin >> v[i];
    string s;
    cin >> s;
    vvint vv;
    initvv(vv, h, w, -1);
    pii A, B;
    rep(i, h) {
      rep(j, w) {
        if (v[i][j] == 'A') {
          A = pii(i, j);
          v[i][j] = '.';
        }
        if (v[i][j] == 'B') {
          B = pii(i, j);
          v[i][j] = '.';
        }
      }
    }
    bfs(A, 0, v, vv);
    int ans = -1;
    pii pos;
    rep(i, h) {
      rep(j, w) {
        // printf("%2d ",vv[i][j]);
        if (vv[i][j] == -1)
          vv[i][j] = INF;
      }
      // cout<<endl;
    }
    rep(i, 1e7) {
      // cout<<i<<" "<<B.F<<" "<<B.S<<endl;
      if (vv[B.F][B.S] <= i) {
        ans = i;
        pos = B;
        break;
      }
      int t = s[i % s.size()] - '0';
      pii ne = pii(B.F + vec[t].F, B.S + vec[t].S);
      if (check(ne.F, ne.S)) {
        B = ne;
      }
    }
    if (ans == -1) {
      cout << "impossible" << endl;
    } else {
      cout << ans << " " << pos.F << " " << pos.S << endl;
    }
    // return;
  }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  mainmain();
}