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

#include <bits/stdc++.h>

using namespace std;

int mx[4] = {0, 1, 0, -1};
int my[4] = {1, 0, -1, 0};

bool memo[50][50][50][50];

int w, h;

bool dfs(pair<int, int> nowL, pair<int, int> nowR, vector<string> &sl,
         vector<string> &sr) {
  stack<pair<pair<int, int>, pair<int, int>>> st;
  st.push(make_pair(nowL, nowR));
  while (!st.empty()) {
    nowL = st.top().first;
    nowR = st.top().second;
    // cout<<nowL.first<<nowL.second<<nowR.first<<nowR.second<<endl;
    st.pop();
    if (memo[nowL.first][nowL.second][nowR.first][nowR.second]) {
      continue;
    } else {
      memo[nowL.first][nowL.second][nowR.first][nowR.second] = 1;
    }
    if (sl[nowL.first][nowL.second] == '%' &&
        sr[nowR.first][nowR.second] == '%') {
      return 1;
    }
    if (sl[nowL.first][nowL.second] == '%') {
      continue;
    }
    if (sr[nowR.first][nowR.second] == '%') {
      continue;
    }
    for (int i = 0; i < 4; ++i) {
      pair<int, int> nextL, nextR;
      nextL.first = nowL.first + my[i];
      nextL.second = nowL.second + mx[i];
      nextR.first = nowR.first + my[i];
      nextR.second = nowR.second - mx[i];
      //------------------------------------
      if (nextL.first < 0 || nextL.first >= h) {
        nextL = nowL;
      }
      if (nextL.second < 0 || nextL.second >= w) {
        nextL = nowL;
      }
      if (sl[nextL.first][nextL.second] == '#') {
        nextL = nowL;
      }
      //-------------------------------------
      if (nextR.first < 0 || nextR.first >= h) {
        nextR = nowR;
      }
      if (nextR.second < 0 || nextR.second >= w) {
        nextR = nowR;
      }
      if (sr[nextR.first][nextR.second] == '#') {
        nextR = nowR;
      }
      st.push(make_pair(nextL, nextR));
    }
  }
  return 0;
  // cout<<nowL.first<<nowL.second<<nowR.first<<nowR.second<<endl;
  /*if(memo[nowL.first][nowL.second][nowR.first][nowR.second]){
      return 0;
  }else{
      memo[nowL.first][nowL.second][nowR.first][nowR.second]=1;
  }
  if(sl[nowL.first][nowL.second]=='%' && sr[nowR.first][nowR.second]=='%'){
      return 1;
  }
  if(sl[nowL.first][nowL.second]=='%'){
      return 0;
  }
  if(sr[nowR.first][nowR.second]=='%'){
      return 0;
  }
  for(int i=0;i<4;++i){
      pair<int,int>nextL,nextR;
      nextL.first=nowL.first+my[i];
      nextL.second=nowL.second+mx[i];
      nextR.first=nowR.first+my[i];
      nextR.second=nowR.second-mx[i];
      //------------------------------------
      if(nextL.first<0 || nextL.first>=h){
          nextL=nowL;
      }
      if(nextL.second<0 || nextL.second>=w){
          nextL=nowL;
      }
      if(sl[nextL.first][nextL.second]=='#'){
          nextL=nowL;
      }
      //-------------------------------------
      if(nextR.first<0 || nextR.first>=h){
          nextR=nowR;
      }
      if(nextR.second<0 || nextR.second>=w){
          nextR=nowR;
      }
      if(sr[nextR.first][nextR.second]=='#'){
          nextR=nowR;
      }
      if(dfs(nextL,nextR,sl,sr))return 1;
  }
  return 0;
  */
}

int main() {
  ios::sync_with_stdio(false);
  while (cin >> w >> h, w) {
    for (int i = 0; i < 50; ++i) {
      for (int j = 0; j < 50; ++j) {
        for (int k = 0; k < 50; ++k) {
          for (int l = 0; l < 50; ++l) {
            memo[i][j][k][l] = 0;
          }
        }
      }
    }
    vector<string> sl(h), sr(h);
    pair<int, int> startL, startR;
    for (int i = 0; i < h; ++i) {
      cin >> sl[i] >> sr[i];
      for (int j = 0; j < w; ++j) {
        if (sl[i][j] == 'L') {
          startL = {i, j};
        }
        if (sr[i][j] == 'R') {
          startR = {i, j};
        }
      }
    }
    if (dfs(startL, startR, sl, sr))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int mx[4] = {0, 1, 0, -1};
int my[4] = {1, 0, -1, 0};

bool memo[50][50][50][50];

int w, h;

bool dfs(pair<int, int> nowL, pair<int, int> nowR, vector<string> &sl,
         vector<string> &sr) {
  // cout<<nowL.first<<nowL.second<<nowR.first<<nowR.second<<endl;
  if (memo[nowL.first][nowL.second][nowR.first][nowR.second]) {
    return 0;
  } else {
    memo[nowL.first][nowL.second][nowR.first][nowR.second] = 1;
  }
  if (sl[nowL.first][nowL.second] == '%' &&
      sr[nowR.first][nowR.second] == '%') {
    return 1;
  }
  if (sl[nowL.first][nowL.second] == '%') {
    return 0;
  }
  if (sr[nowR.first][nowR.second] == '%') {
    return 0;
  }
  for (int i = 0; i < 4; ++i) {
    pair<int, int> nextL, nextR;
    nextL.first = nowL.first + my[i];
    nextL.second = nowL.second + mx[i];
    nextR.first = nowR.first + my[i];
    nextR.second = nowR.second - mx[i];
    //------------------------------------
    if (nextL.first < 0 || nextL.first >= h) {
      nextL = nowL;
    }
    if (nextL.second < 0 || nextL.second >= w) {
      nextL = nowL;
    }
    if (sl[nextL.first][nextL.second] == '#') {
      nextL = nowL;
    }
    //-------------------------------------
    if (nextR.first < 0 || nextR.first >= h) {
      nextR = nowR;
    }
    if (nextR.second < 0 || nextR.second >= w) {
      nextR = nowR;
    }
    if (sr[nextR.first][nextR.second] == '#') {
      nextR = nowR;
    }
    if (dfs(nextL, nextR, sl, sr))
      return 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  while (cin >> w >> h, w) {
    for (int i = 0; i < 50; ++i) {
      for (int j = 0; j < 50; ++j) {
        for (int k = 0; k < 50; ++k) {
          for (int l = 0; l < 50; ++l) {
            memo[i][j][k][l] = 0;
          }
        }
      }
    }
    vector<string> sl(h), sr(h);
    pair<int, int> startL, startR;
    for (int i = 0; i < h; ++i) {
      cin >> sl[i] >> sr[i];
      for (int j = 0; j < w; ++j) {
        if (sl[i][j] == 'L') {
          startL = {i, j};
        }
        if (sr[i][j] == 'R') {
          startR = {i, j};
        }
      }
    }
    if (dfs(startL, startR, sl, sr))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}