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

#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>

#pragma warning(disable : 4996)

using namespace std;

const int dx1[4] = {0, 1, 0, -1};
const int dy1[4] = {-1, 0, 1, 0};
const int dx2[4] = {0, -1, 0, 1};
const int dy2[4] = {-1, 0, 1, 0};

int H, W;
string L[50], R[50];

bool used[50][50][50][50];

bool over(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

bool solve(int x1, int y1, int x2, int y2) {
  queue<tuple<int, int, int, int>> que;

  que.push(make_tuple(y1, x1, y2, x2));

  used[y1][x1][y2][x2] = true;

  while (!que.empty()) {
    int ly = get<0>(que.front());
    int lx = get<1>(que.front());
    int ry = get<2>(que.front());
    int rx = get<3>(que.front());

    que.pop();

    if (L[ly][lx] == '%' && R[ry][rx] == '%')
      return true;

    if (L[ly][lx] == '%')
      continue;
    if (R[ry][rx] == '%')
      continue;

    bool ok = false;

    for (int i = 0; i < 4; i++) {
      int ltx = lx + dx1[i];
      int lty = ly + dy1[i];
      int rtx = rx + dx2[i];
      int rty = ry + dy2[i];

      if (!over(ltx, lty)) {
        ltx -= dx1[i];
        lty -= dy1[i];
      }

      if (!over(rtx, rty)) {
        rtx -= dx2[i];
        rty -= dy2[i];
      }

      if (L[lty][ltx] == '#') {
        ltx -= dx1[i];
        lty -= dy1[i];
      }

      if (R[rty][rtx] == '#') {
        rtx -= dx2[i];
        rty -= dy2[i];
      }

      if (!used[lty][ltx][rty][rtx]) {
        used[lty][ltx][rty][rtx] = true;

        que.push(make_tuple(lty, ltx, rty, rtx));
      }
    }
  }

  return false;
}

int main() {
  while (true) {
    scanf("%d", &W);
    scanf("%d", &H);

    if (W == 0 && H == 0)
      break;

    for (int i = 0; i < 50; i++)
      L[i] = "";
    for (int i = 0; i < 50; i++)
      R[i] = "";

    for (int i = 0; i < H; i++)
      cin >> L[i] >> R[i];

    int lx, ly, rx, ry;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (L[i][j] == 'L') {
          lx = j;
          ly = i;
        }

        if (R[i][j] == 'R') {
          rx = j;
          ry = i;
        }
      }
    }

    memset(used, false, sizeof(used));

    printf("%s\\n", solve(lx, ly, rx, ry) ? "Yes" : "No");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>

#pragma warning(disable : 4996)

using namespace std;

const int dx1[4] = {0, 1, 0, -1};
const int dy1[4] = {-1, 0, 1, 0};
const int dx2[4] = {0, -1, 0, 1};
const int dy2[4] = {-1, 0, 1, 0};

int H, W;
string L[50], R[50];

bool used[50][50][50][50];

bool over(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

bool solve(int x1, int y1, int x2, int y2) {
  queue<tuple<int, int, int, int>> que;

  que.push(make_tuple(y1, x1, y2, x2));

  used[y1][x1][y2][x2] = true;

  while (!que.empty()) {
    int ly = get<0>(que.front());
    int lx = get<1>(que.front());
    int ry = get<2>(que.front());
    int rx = get<3>(que.front());

    if (L[ly][lx] == '%' && R[ry][rx] == '%')
      return true;

    if (L[ly][lx] == '%')
      continue;
    if (R[ry][rx] == '%')
      continue;

    bool ok = false;

    for (int i = 0; i < 4; i++) {
      int ltx = lx + dx1[i];
      int lty = ly + dy1[i];
      int rtx = rx + dx2[i];
      int rty = ry + dy2[i];

      if (over(ltx, lty) && over(rtx, rty)) {
        if (L[lty][ltx] == '#') {
          ltx -= dx1[i];
          lty -= dy1[i];
        }

        if (R[rty][rtx] == '#') {
          rtx -= dx2[i];
          rty -= dy2[i];
        }

        if (!used[lty][ltx][rty][rtx]) {
          used[lty][ltx][rty][rtx] = true;

          que.push(make_tuple(lty, ltx, rty, rtx));
        }
      }
    }
  }

  return false;
}

int main() {
  while (true) {
    scanf("%d", &W);
    scanf("%d", &H);

    if (W == 0 && H == 0)
      break;

    for (int i = 0; i < H; i++)
      cin >> L[i] >> R[i];

    int lx, ly, rx, ry;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (L[i][j] == 'L') {
          lx = j;
          ly = i;
        }

        if (R[i][j] == 'R') {
          rx = j;
          ry = i;
        }
      }
    }

    memset(used, false, sizeof(used));

    printf("%s\\n", solve(lx, ly, rx, ry) ? "Yes" : "No");
  }

  return 0;
}