Problem p01271 (C++) — buggy submission, verdict: Runtime Error

//????????????????????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, pii> ver;
typedef pair<int, ver> piv;
int dx[2][4] = {{1, 0, -1, 0}, {-1, 0, 1, 0}};
int dy[] = {0, 1, 0, -1};
int dp[51][51][51][51];

bool range(pii p, int w, int h) {
  if (0 <= p.first && p.first < w && 0 <= p.second && p.second < h)
    return true;
  return false;
}
int main(int argc, char *argv[]) {
  int w, h;
  while (cin >> w >> h, w) {
    vector<string> grid1(h), grid2(h);
    pii s[2], g[2];
    for (int i = 0; i < h; i++) {
      cin >> grid1[i] >> grid2[i];
      for (int j = 0; j < w; j++) {
        if (grid1[i][j] == 'L')
          s[0].first = j, s[0].second = i;
        if (grid1[i][j] == '%')
          g[0].first = j, g[0].second = i;
        if (grid2[i][j] == 'R')
          s[1].first = j, s[1].second = i;
        if (grid2[i][j] == '%')
          g[1].first = j, g[1].second = i;
      }
    }
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        for (int k = 0; k < h; k++)
          for (int l = 0; l < w; l++)
            dp[i][j][k][l] = 1e9;

    dp[s[0].second][s[0].first][s[1].second][s[1].first] = 0;
    queue<piv> que;
    que.push(piv(0, ver(s[0], s[1])));
    while (!que.empty()) {
      pii u = que.front().second.first;
      pii v = que.front().second.second;
      int c = que.front().first;
      que.pop();
      if (u == g[0] && v == g[1])
        break;
      for (int i = 0; i < 4; i++) {
        pii a = u, b = v;
        a.first += dx[0][i];
        a.second += dy[i];
        b.first += dx[1][i];
        b.second += dy[i];
        if (!range(a, w, h) || grid1[a.second][a.first] == '#')
          a = u;
        if (!range(b, w, h) || grid2[b.second][b.first] == '#')
          b = v;
        if (a != g[0] || b != g[1]) {
          if (a == g[0] || b == g[1])
            continue;
        }
        if (dp[a.second][a.first][b.second][b.first] > c + 1) {
          dp[a.second][a.first][b.second][b.first] = c + 1;
          que.push(piv(c + 1, ver(a, b)));
        }
      }
    }

    if (dp[g[0].second][g[0].first][g[1].second][g[1].first] != 1e9)
      std::cout << "Yes" << std::endl;
    else
      std::cout << "No" << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

//????????????????????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, pii> ver;
typedef pair<int, ver> piv;
int dx[2][4] = {{1, 0, -1, 0}, {-1, 0, 1, 0}};
int dy[] = {0, 1, 0, -1};

bool range(pii p, int w, int h) {
  if (0 <= p.first && p.first < w && 0 <= p.second && p.second < h)
    return true;
  return false;
}
int main(int argc, char *argv[]) {
  int w, h;
  while (cin >> w >> h, w) {
    vector<string> grid1(h), grid2(h);
    int sx[2], sy[2], gx[2], gy[2];
    for (int i = 0; i < h; i++) {
      cin >> grid1[i] >> grid2[i];
      for (int j = 0; j < w; j++) {
        if (grid1[i][j] == 'L')
          sx[0] = j, sy[0] = i;
        if (grid1[i][j] == '%')
          gx[0] = j, gy[0] = i;
        if (grid2[i][j] == 'R')
          sx[1] = j, sy[1] = i;
        if (grid2[i][j] == '%')
          gx[1] = j, gy[1] = i;
      }
    }
    int dp[h][w][h][w];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        for (int k = 0; k < h; k++)
          for (int l = 0; l < w; l++)
            dp[i][j][k][l] = 1e9;
    dp[sy[0]][sx[0]][sy[1]][sx[1]] = 0;
    queue<piv> que;
    que.push(piv(0, ver(pii(sx[0], sy[0]), pii(sx[1], sy[1]))));
    while (!que.empty()) {
      pii u = que.front().second.first;
      pii v = que.front().second.second;
      int c = que.front().first;
      que.pop();
      for (int i = 0; i < 4; i++) {
        pii a = u, b = v;
        a.first += dx[0][i];
        a.second += dy[i];
        b.first += dx[1][i];
        b.second += dy[i];
        if (!range(a, w, h))
          a = u;
        if (grid1[a.second][a.first] == '#')
          a = u;
        if (!range(b, w, h))
          b = v;
        if (grid2[b.second][b.first] == '#')
          b = v;
        if (dp[a.second][a.first][b.second][b.first] > c + 1) {
          dp[a.second][a.first][b.second][b.first] = c + 1;
          que.push(piv(c + 1, ver(a, b)));
        }
      }
    }
    if (dp[gy[0]][gx[0]][gy[1]][gx[1]] != 1e9)
      std::cout << "Yes" << std::endl;
    else
      std::cout << "No" << std::endl;
  }
  return 0;
}