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

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \\
  rep(_, v.size()) cout << " " << v[_];                                        \\
  cout << endl;
#define show2d(v)                                                              \\
  rep(_, v.size()) {                                                           \\
    rep(__, v[_].size()) cout << " " << v[_][__];                              \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 1e18 + 10 : 1e9 + 10;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; // RDLU
int dp[55][55][2][1333];
int bef[1030][55][55];
signed main() {
  rep(i, 55) rep(j, 55) rep(q, 2) rep(k, 1333) dp[i][j][q][k] = inf;

  int n, m;
  cin >> m >> n;
  vs in(n);
  rep(i, n) cin >> in[i];

  vector<vs> ini(2, in);

  rep(i, n) rep(j, m) {
    if (in[i][j] == '%')
      ini[1][i][j] = '#';
    if (in[i][j] == '_')
      ini[1][i][j] = '#';
    if (in[i][j] == '^')
      ini[0][i][j] = '#';
    if (islower(in[i][j]))
      ini[1][i][j] = '#';
    if (isupper(in[i][j])) {
      ini[0][i][j] = '#';
      ini[1][i][j] = tolower(in[i][j]);
    }
    if (in[i][j] == '&')
      ini[1][i][j] = '#';
  }

  int q;
  cin >> q;
  vector<vs> w(q, vs(n));
  rep(i, q) rep(j, n) cin >> w[i][j];

  queue<tp> que;
  rep(i, n) rep(j, m) if (in[i][j] == '%') {
    que.push(tp(i, j, 0, 0));
    dp[i][j][0][0] = 0;
  }

  rep(C, 1 << q) {
    rep(c, 10) if (C & 1 << c) rep(i, n) rep(j, m) if (w[c][i][j] == '*')
        bef[C][i][j] ^= 1;
  }

  int out = inf;
  while (!que.empty()) {
    int x, y, f, s;
    tie(x, y, f, s) = que.front();
    // cout<<x<<" "<<y<<" "<<f<<" "<<s<<" "<<dp[x][y][f][s]<<endl;
    que.pop();

    vector<vs> now(2, vs(3, string(3, 'a')));
    // show2d(now);
    rep(i, 2) rep(j, 3) rep(k, 3) now[i][j][k] = ini[i][x + j - 1][y + k - 1];
    // show2d(now);
    rep(i, 3) rep(j, 3) if (bef[s][x + i - 1][y + j - 1]) {
      swap(now[0][i][j], now[1][i][j]);
    }
    if (now[f][1][1] == '&') {
      out = dp[x][y][f][s];
      break;
    }
    rep(k, 4) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (now[f][dx[k] + 1][dy[k] + 1] == '#')
        continue;
      if (dp[x][y][f][s] + 1 < dp[nx][ny][f][s]) {
        dp[nx][ny][f][s] = dp[x][y][f][s] + 1;
        que.push(tp(nx, ny, f, s));
      }
    }
    if (isalpha(now[f][1][1])) {
      int ns = s ^ 1 << (now[f][1][1] - 'a');
      int nf = f;
      if (w[now[f][1][1] - 'a'][x][y] == '*')
        nf ^= 1;
      if (dp[x][y][f][s] + 1 < dp[x][y][nf][ns]) {
        dp[x][y][nf][ns] = dp[x][y][f][s] + 1;
        que.push(tp(x, y, nf, ns));
      }
    }
    if (now[f][1][1] == '|') {
      int nf = f ^ 1;
      if (dp[x][y][f][s] + 1 < dp[x][y][nf][s]) {
        dp[x][y][nf][s] = dp[x][y][f][s] + 1;
        que.push(tp(x, y, nf, s));
      }
    }
  }
  if (out == inf)
    out = -1;
  cout << out << endl;
}


Error: TLE

Fixed submission:

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \\
  rep(_, v.size()) cout << " " << v[_];                                        \\
  cout << endl;
#define show2d(v)                                                              \\
  rep(_, v.size()) {                                                           \\
    rep(__, v[_].size()) cout << " " << v[_][__];                              \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 1e18 + 10 : 1e9 + 10;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; // RDLU
int dp[55][55][2][1333];
int bef[1030][55][55];
signed main() {
  rep(i, 55) rep(j, 55) rep(q, 2) rep(k, 1333) dp[i][j][q][k] = inf;

  int n, m;
  cin >> m >> n;
  vs in(n);
  rep(i, n) cin >> in[i];

  vector<vs> ini(2, in);

  rep(i, n) rep(j, m) {
    if (in[i][j] == '%')
      ini[1][i][j] = '#';
    if (in[i][j] == '_')
      ini[1][i][j] = '#';
    if (in[i][j] == '^')
      ini[0][i][j] = '#';
    if (islower(in[i][j]))
      ini[1][i][j] = '#';
    if (isupper(in[i][j])) {
      ini[0][i][j] = '#';
      ini[1][i][j] = tolower(in[i][j]);
    }
    if (in[i][j] == '&')
      ini[1][i][j] = '#';
  }

  int q;
  cin >> q;
  vector<vs> w(q, vs(n));
  rep(i, q) rep(j, n) cin >> w[i][j];

  queue<tp> que;
  rep(i, n) rep(j, m) if (in[i][j] == '%') {
    que.push(tp(i, j, 0, 0));
    dp[i][j][0][0] = 0;
  }

  rep(C, 1 << q) {
    rep(c, 10) if (C & 1 << c) rep(i, n) rep(j, m) if (w[c][i][j] == '*')
        bef[C][i][j] ^= 1;
  }

  int out = inf;
  while (!que.empty()) {
    int x, y, f, s;
    tie(x, y, f, s) = que.front();
    //		cout<<x<<" "<<y<<" "<<f<<" "<<s<<" "<<dp[x][y][f][s]<<endl;
    que.pop();

    vector<vs> now = ini;
    rep(i, n) rep(j, m) if (bef[s][i][j]) swap(now[0][i][j], now[1][i][j]);

    if (now[f][x][y] == '&') {
      out = dp[x][y][f][s];
      break;
    }
    rep(k, 4) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (now[f][nx][ny] == '#')
        continue;
      if (dp[x][y][f][s] + 1 < dp[nx][ny][f][s]) {
        dp[nx][ny][f][s] = dp[x][y][f][s] + 1;
        que.push(tp(nx, ny, f, s));
      }
    }
    if (isalpha(now[f][x][y])) {
      int ns = s ^ 1 << (now[f][x][y] - 'a');
      int nf = f;
      if (w[now[f][x][y] - 'a'][x][y] == '*')
        nf ^= 1;
      if (dp[x][y][f][s] + 1 < dp[x][y][nf][ns]) {
        dp[x][y][nf][ns] = dp[x][y][f][s] + 1;
        que.push(tp(x, y, nf, ns));
      }
    }
    if (now[f][x][y] == '|') {
      int nf = f ^ 1;
      if (dp[x][y][f][s] + 1 < dp[x][y][nf][s]) {
        dp[x][y][nf][s] = dp[x][y][f][s] + 1;
        que.push(tp(x, y, nf, s));
      }
    }
  }
  if (out == inf)
    out = -1;
  cout << out << endl;
}