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

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
int d1[4] = {1, 0, -1, 0};
int d2[4] = {0, 1, 0, -1};
int d3[4] = {0, -1, 0, 1};
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    char fi1[52][52];
    char fi2[52][52];
    rep(i, 52) {
      rep(j, 52) { fi1[i][j] = fi2[i][j] = '#'; }
    }
    string s, t;
    P s1, s2, g1, g2;
    rep1(i, h) {
      cin >> s >> t;
      rep1(j, w) {
        fi1[i][j] = s[j - 1];
        if (fi1[i][j] == '%') {
          g1 = {i, j};
        } else if (fi1[i][j] == 'L') {
          s1 = {i, j};
        }
      }
      rep1(j, w) {
        fi2[i][j] = t[j - 1];
        if (fi2[i][j] == '%') {
          g2 = {i, j};
        } else if (fi2[i][j] == 'R') {
          s2 = {i, j};
        }
      }
    }
    map<pair<P, P>, int> used;
    queue<pair<P, P>> v;
    bool f = false;
    v.push({s1, s2});
    used[{s1, s2}] = 1;
    while (!v.empty()) {
      pair<P, P> now = v.front();
      P n1 = now.first;
      P n2 = now.second;
      v.pop();
      if (n1 == g1 && n2 == g2) {
        f = true;
        break;
      } else if (n1 == g1 || n2 == g2) {
        continue;
      }
      rep(i, 4) {
        P nex1 = {n1.first + d1[i], n1.second + d2[i]};
        P nex2 = {n2.first + d1[i], n2.second + d3[i]};
        if (fi1[nex1.first][nex1.second] == '#') {
          nex1 = n1;
        }
        if (fi2[nex2.first][nex2.second] == '#') {
          nex2 = n2;
        }
        if (used[{nex1, nex2}] != 1) {
          used[{nex1, nex2}] = 1;
          v.push({nex1, nex2});
        }
      }
    }
    if (f) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
int d1[4] = {1, 0, -1, 0};
int d2[4] = {0, 1, 0, -1};
int d3[4] = {0, -1, 0, 1};
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    char fi1[52][52];
    char fi2[52][52];
    rep(i, 52) {
      rep(j, 52) { fi1[i][j] = fi2[i][j] = '#'; }
    }
    string s, t;
    P s1, s2, g1, g2;
    rep1(i, h) {
      cin >> s >> t;
      rep1(j, w) {
        fi1[i][j] = s[j - 1];
        if (fi1[i][j] == '%') {
          g1 = {i, j};
        } else if (fi1[i][j] == 'L') {
          s1 = {i, j};
        }
      }
      rep1(j, w) {
        fi2[i][j] = t[j - 1];
        if (fi2[i][j] == '%') {
          g2 = {i, j};
        } else if (fi2[i][j] == 'R') {
          s2 = {i, j};
        }
      }
    }
    map<pair<P, P>, int> used;
    vector<pair<P, P>> v;
    bool f = false;
    v.push_back({s1, s2});
    used[{s1, s2}] = 1;
    while (!v.empty()) {
      P n1 = v[0].first;
      P n2 = v[0].second;
      v.erase(v.begin());
      if (n1 == g1 && n2 == g2) {
        f = true;
        break;
      } else if (n1 == g1 || n2 == g2) {
        continue;
      }
      rep(i, 4) {
        P nex1 = {n1.first + d1[i], n1.second + d2[i]};
        P nex2 = {n2.first + d1[i], n2.second + d3[i]};
        if (fi1[nex1.first][nex1.second] == '#') {
          nex1 = n1;
        }
        if (fi2[nex2.first][nex2.second] == '#') {
          nex2 = n2;
        }
        if (used[{nex1, nex2}] != 1) {
          used[{nex1, nex2}] = 1;
          v.push_back({nex1, nex2});
        }
      }
    }
    if (f) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}