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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<int, int>
#define endl "\\n"
#define MAXN 100005
using namespace std;

int n, a, b, c;
string s[MAXN];
vector<char> v;

void solve() {
  cin >> n >> a >> b >> c;
  for (int i = 1; i <= n; i++)
    cin >> s[i];
  for (int i = 1; i <= n; i++) {
    // cout<<a<<" "<<b<<" "<<c<<endl;
    if (s[i] == "AB") {
      if (a == 1 && b == 1 && c == 0) {
        if (i < n) {
          if (s[i + 1] == "AB" || s[i + 1] == "BC") {
            v.pb('B');
            a--;
            b++;
          } else if (s[i + 1] == "AC") {
            v.pb('A');
            a++;
            b--;
          }
        } else
          v.pb('A');
      } else if (a == 0 && b == 0) {
        cout << "No" << endl;
        return;
      } else {
        if (a >= b) {
          v.pb('B');
          a--;
          b++;
        } else {
          v.pb('A');
          a++;
          b--;
        }
      }
    } else if (s[i] == "AC") {
      if (a == 1 && b == 0 && c == 1) {
        if (i < n) {
          if (s[i + 1] == "AC" || s[i + 1] == "BC") {
            v.pb('C');
            a--;
            c++;
          } else if (s[i + 1] == "AB") {
            v.pb('A');
            a++;
            c--;
          }
        } else
          v.pb('A');
      } else if (a == 0 && c == 0) {
        cout << "No" << endl;
        return;
      } else {
        if (a >= c) {
          v.pb('C');
          a--;
          c++;
        } else {
          v.pb('A');
          a++;
          c--;
        }
      }
    } else if (s[i] == "BC") {
      if (a == 0 && b == 1 && c == 1) {
        if (i < n) {
          if (s[i + 1] == "BC" || s[i + 1] == "AB") {
            v.pb('B');
            c--;
            b++;
          } else if (s[i + 1] == "AC") {
            v.pb('C');
            c++;
            b--;
          }
        } else
          v.pb('C');
      } else if (c == 0 && b == 0) {
        cout << "No" << endl;
        return;
      } else {
        if (c >= b) {
          v.pb('B');
          c--;
          b++;
        } else {
          v.pb('C');
          c++;
          b--;
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<ll, ll>
#define endl "\\n"
#define MAXN 100005
using namespace std;

ll n, a, b, c, flag;
vector<char> v;
map<pair<pi, pi>, ll> vis;
map<pair<pi, pi>, pair<pi, pi>> par;
string s[MAXN];
pair<pi, pi> x;

void bfs() {
  vis[{{a, b}, {c, 1}}] = 1;
  queue<pair<pi, pi>> q;
  q.push({{a, b}, {c, 1}});
  while (!q.empty()) {
    pair<pi, pi> p = q.front();
    ll d = p.ff.ff, e = p.ff.ss, f = p.ss.ff, i = p.ss.ss;
    // cout<<d<<" "<<e<<" "<<f<<" "<<i<<endl;
    q.pop();
    if (i == n + 1) {
      flag = 1;
      x = p;
      break;
    }
    if (s[i] == "AB") {
      if (!vis[{{d - 1, e + 1}, {f, i}}]) {
        if (d - 1 >= 0) {
          par[{{d - 1, e + 1}, {f, i + 1}}] = {{d, e}, {f, i}};
          q.push({{d - 1, e + 1}, {f, i + 1}});
          vis[{{d - 1, e + 1}, {f, i}}] = 1;
        }
      }
      if (!vis[{{d + 1, e - 1}, {f, i}}]) {
        if (e - 1 >= 0) {
          par[{{d + 1, e - 1}, {f, i + 1}}] = {{d, e}, {f, i}};
          q.push({{d + 1, e - 1}, {f, i + 1}});
          vis[{{d + 1, e - 1}, {f, i}}] = 1;
        }
      }
    } else if (s[i] == "AC") {
      if (!vis[{{d - 1, e}, {f + 1, i}}]) {
        if (d - 1 >= 0) {
          par[{{d - 1, e}, {f + 1, i + 1}}] = {{d, e}, {f, i}};
          q.push({{d - 1, e}, {f + 1, i + 1}});
          vis[{{d - 1, e}, {f + 1, i}}] = 1;
        }
      }
      if (!vis[{{d + 1, e}, {f - 1, i}}]) {
        if (f - 1 >= 0) {
          par[{{d + 1, e}, {f - 1, i + 1}}] = {{d, e}, {f, i}};
          q.push({{d + 1, e}, {f - 1, i + 1}});
          vis[{{d + 1, e}, {f - 1, i}}] = 1;
        }
      }
    } else if (s[i] == "BC") {
      if (!vis[{{d, e - 1}, {f + 1, i}}]) {
        if (e - 1 >= 0) {
          par[{{d, e - 1}, {f + 1, i + 1}}] = {{d, e}, {f, i}};
          q.push({{d, e - 1}, {f + 1, i + 1}});
          vis[{{d, e - 1}, {f + 1, i}}] = 1;
        }
      }
      if (!vis[{{d, e + 1}, {f - 1, i}}]) {
        if (f - 1 >= 0) {
          par[{{d, e + 1}, {f - 1, i + 1}}] = {{d, e}, {f, i}};
          q.push({{d, e + 1}, {f - 1, i + 1}});
          vis[{{d, e + 1}, {f - 1, i}}] = 1;
        }
      }
    }
  }
  if (flag == 1) {
    cout << "Yes" << endl;
    while (1) {

      if (x.ff.ff == a && x.ff.ss == b && x.ss.ff == c && x.ss.ss == 1)
        break;
      pair<pi, pi> t = par[x];
      if (t.ff.ff - x.ff.ff == 1 && x.ff.ss - t.ff.ss == 1)
        v.pb('B');
      else if (x.ff.ff - t.ff.ff == 1 && t.ff.ss - x.ff.ss == 1)
        v.pb('A');
      else if (t.ff.ss - x.ff.ss == 1 && x.ss.ff - t.ss.ff == 1)
        v.pb('C');
      else if (x.ff.ss - t.ff.ss == 1 && t.ss.ff - x.ss.ff == 1)
        v.pb('B');
      else if (t.ff.ff - x.ff.ff == 1 && x.ss.ff - t.ss.ff == 1)
        v.pb('C');
      else if (x.ff.ff - t.ff.ff == 1 && t.ss.ff - x.ss.ff == 1)
        v.pb('A');
      x = t;
    }
    reverse(v.begin(), v.end());
    for (ll i = 0; i < v.size(); i++)
      cout << v[i] << endl;
  } else
    cout << "No" << endl;
}

void solve() {
  cin >> n >> a >> b >> c;
  for (ll i = 1; i <= n; i++)
    cin >> s[i];
  bfs();
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}