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

#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
typedef long long LL;
int H, W;
int A[51][51];
int B[51][51];
int sax, say, tax, tay;
int sbx, sby, tbx, tby;
bool used[51][51][51][51];
typedef pair<int, int> P;
typedef pair<P, P> PP;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

P move(P now, int x, int y) {
  if (now.first + x < 0) {
    return now;
  }
  if (now.first + x >= W) {
    return now;
  }
  if (now.second + y < 0) {
    return now;
  }
  if (now.second + y >= H) {
    return now;
  }
  if (A[now.first + x][now.second] != 1) {
    now.first += x;
  }
  if (A[now.first][now.second + y] != 1) {
    now.second += y;
  }
  return now;
}

P move2(P now, int x, int y) {
  if (now.first + x < 0) {
    return now;
  }
  if (now.first + x >= W) {
    return now;
  }
  if (now.second + y < 0) {
    return now;
  }
  if (now.second + y >= H) {
    return now;
  }
  if (B[now.first + x][now.second] != 1) {
    now.first += x;
  }
  if (B[now.first][now.second + y] != 1) {
    now.second += y;
  }
  return now;
}

int main() {
  int c = 0;
  while (cin >> W >> H, W) {
    // c++;
    // if(c>=2)break;
    // vector<int>
    REP(i, H) {
      string s;
      cin >> s;
      REP(j, W) {
        if (s[j] == 'L') {
          sax = j;
          say = i;
        }
        if (s[j] == '%') {
          tax = j;
          tay = i;
        }
        A[j][i] = 0;
        if (s[j] == '#') {
          A[j][i] = 1;
        }
      }
      cin >> s;
      REP(j, W) {
        if (s[j] == 'R') {
          sbx = j;
          sby = i;
        }
        if (s[j] == '%') {
          tbx = j;
          tby = i;
        }
        B[j][i] = 0;
        if (s[j] == '#') {
          B[j][i] = 1;
        }
      }
    }
    // cout<<"input_ok"<<endl;
    // cout<<tax<<" "<<tay<<" "<<tbx<<" "<<tby<<endl;
    //????????????bfs
    REP(i, 50) {
      REP(j, 50) {
        REP(k, 50) {
          REP(l, 50) { used[i][j][k][l] = false; }
        }
      }
    }

    used[sax][say][sbx][sby] = true;
    queue<PP> v;
    // vector<PP>v;
    v.push(PP(P(sax, say), P(sbx, sby)));
    int now = 0;
    while (!v.empty()) {
      // P l=v[now].first;
      // P r=v[now].second;
      P l = v.front().first;
      P r = v.front().second;
      v.pop();
      used[l.first][l.second][r.first][r.second] = true;
      // cout<<l.first<<" "<<l.second<<" "<<r.first<<" "<<r.second<<endl;
      REP(i, 4) {
        P ll = move(l, dx[i], dy[i]);
        P rr = move2(r, -dx[i], dy[i]);
        if (used[ll.first][ll.second][rr.first][rr.second] == false) {
          used[ll.first][ll.second][rr.first][rr.second] = true;
          /*if(now>=50*50*50*50){
          cout<<c<<"ok"<<ll.first<<" "<<ll.second<<" "<<rr.first<<"
          "<<rr.second<<endl;
          }*/
          if ((ll.first == tax) && (ll.second == tay)) {
            used[ll.first][ll.second][rr.first][rr.second] = true;
            continue;
          }
          if ((rr.first == tbx) && (rr.second == tby)) {
            used[ll.first][ll.second][rr.first][rr.second] = true;
            continue;
          }
          v.push(PP(ll, rr));
        }
      }
      now++;
    }

    // cout<<endl;
    if (used[tax][tay][tbx][tby] == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
typedef long long LL;
int H, W;
int A[100][100];
int B[100][100];
int sax, say, tax, tay;
int sbx, sby, tbx, tby;
bool used[51][51][51][51];
typedef pair<int, int> P;
typedef pair<P, P> PP;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

P move(P now, int x, int y) {
  if (now.first + x < 0) {
    return now;
  }
  if (now.first + x >= W) {
    return now;
  }
  if (now.second + y < 0) {
    return now;
  }
  if (now.second + y >= H) {
    return now;
  }
  if (A[now.first + x][now.second] != 1) {
    now.first += x;
  }
  if (A[now.first][now.second + y] != 1) {
    now.second += y;
  }
  return now;
}

P move2(P now, int x, int y) {
  if (now.first + x < 0) {
    return now;
  }
  if (now.first + x >= W) {
    return now;
  }
  if (now.second + y < 0) {
    return now;
  }
  if (now.second + y >= H) {
    return now;
  }
  if (B[now.first + x][now.second] != 1) {
    now.first += x;
  }
  if (B[now.first][now.second + y] != 1) {
    now.second += y;
  }
  return now;
}

int main() {
  int c = 0;
  while (cin >> W >> H, W) {
    // c++;
    // if(c>=2)break;
    // vector<int>
    REP(i, H) {
      string s;
      cin >> s;
      REP(j, W) {
        if (s[j] == 'L') {
          sax = j;
          say = i;
        }
        if (s[j] == '%') {
          tax = j;
          tay = i;
        }
        A[j][i] = 0;
        if (s[j] == '#') {
          A[j][i] = 1;
        }
      }
      cin >> s;
      REP(j, W) {
        if (s[j] == 'R') {
          sbx = j;
          sby = i;
        }
        if (s[j] == '%') {
          tbx = j;
          tby = i;
        }
        B[j][i] = 0;
        if (s[j] == '#') {
          B[j][i] = 1;
        }
      }
    }
    // cout<<"input_ok"<<endl;
    // cout<<tax<<" "<<tay<<" "<<tbx<<" "<<tby<<endl;
    //????????????bfs
    REP(i, 50) {
      REP(j, 50) {
        REP(k, 50) {
          REP(l, 50) { used[i][j][k][l] = false; }
        }
      }
    }

    used[sax][say][sbx][sby] = true;
    vector<PP> v;
    v.push_back(PP(P(sax, say), P(sbx, sby)));
    int now = 0;
    while (now < v.size()) {
      // cout<<"now"<<now;
      P l = v[now].first;
      P r = v[now].second;
      used[l.first][l.second][r.first][r.second] = true;
      // cout<<l.first<<" "<<l.second<<" "<<r.first<<" "<<r.second<<endl;
      REP(i, 4) {
        P ll = move(l, dx[i], dy[i]);
        P rr = move2(r, -dx[i], dy[i]);
        if (used[ll.first][ll.second][rr.first][rr.second] == false) {
          used[ll.first][ll.second][rr.first][rr.second] = true;
          /*if(now>=50*50*50*50){
          cout<<c<<"ok"<<ll.first<<" "<<ll.second<<" "<<rr.first<<"
          "<<rr.second<<endl;
          }*/
          if ((ll.first == tax) && (ll.second == tay)) {
            used[ll.first][ll.second][rr.first][rr.second] = true;
            continue;
          }
          if ((rr.first == tbx) && (rr.second == tby)) {
            used[ll.first][ll.second][rr.first][rr.second] = true;
            continue;
          }
          v.push_back(PP(ll, rr));
        }
      }
      now++;
    }

    // cout<<endl;
    if (used[tax][tay][tbx][tby] == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}