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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#define debug cout

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n); i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int used[50][50][50][50], w, h;
string l[50], r[50];
int v[100000][4], vi;

int main() {
  IL {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    rep(i, h) { cin >> l[i] >> r[i]; }
    vint start(4);
    vint goal(4);
    rep(i, h) rep(j, w) {
      if (l[i][j] == 'L') {
        start[0] = i;
        start[1] = j;
      }
      if (r[i][j] == 'R') {
        start[2] = i;
        start[3] = j;
      }
      if (l[i][j] == '%') {
        goal[0] = i;
        goal[1] = j;
      }
      if (r[i][j] == '%') {
        goal[2] = i;
        goal[3] = j;
      }
    }
    vi = 0;
    v[0][0] = start[0];
    v[0][1] = start[1];
    v[0][2] = start[2];
    v[0][3] = start[3];
    memset(used, 0, sizeof(used));
    used[start[0]][start[1]][start[2]][start[3]] = 1;
    while (vi >= 0) {
      int now[4];
      rep(i, 4) now[i] = v[vi][i];
      --vi;
      // debug<<vi<<" "<<now[0]<<" "<<now[1]<<" "<<now[2]<<" "<<now[3]<<endl;
      rep(i, 4) {
        int next[4];
        next[0] = now[0] + dy[i];
        next[1] = now[1] + dx[i];
        next[2] = now[2] + dy[i];
        next[3] = now[3] - dx[i];
        // debug<<vi<<" "<<now[0]<<" "<<now[1]<<" "<<now[2]<<" "<<now[3]<<endl;
        // debug<<vi<<" "<<next[0]<<" "<<next[1]<<" "<<next[2]<<"
        // "<<next[3]<<endl;
        if (next[0] < 0 || h <= next[0] || l[next[0]][now[1]] == '#')
          next[0] = now[0];
        if (next[1] < 0 || w <= next[1] || l[now[0]][next[1]] == '#')
          next[1] = now[1];
        if (next[2] < 0 || h <= next[2] || r[next[2]][now[3]] == '#')
          next[2] = now[2];
        if (next[3] < 0 || w <= next[3] || r[now[2]][next[3]] == '#')
          next[3] = now[3];
        // debug<<vi<<" "<<next[0]<<" "<<next[1]<<" "<<next[2]<<"
        // "<<next[3]<<endl;
        if (used[next[0]][next[1]][next[2]][next[3]] == 1)
          continue;
        used[next[0]][next[1]][next[2]][next[3]] = 1;
        if (l[next[0]][next[1]] == '%')
          continue;
        if (r[next[2]][next[3]] == '%')
          continue;
        ++vi;
        rep(j, 4) v[vi][j] = next[j];
        // debug<<vi<<" "<<next[0]<<" "<<next[1]<<" "<<next[2]<<"
        // "<<next[3]<<endl;
      }
      // debug<<endl;
    }
    if (used[goal[0]][goal[1]][goal[2]][goal[3]])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#define debug cout

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n); i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int used[50][50][50][50], w, h;
string l[50], r[50];
int v[1000000][4], vi, vs;

int main() {
  IL {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    rep(i, h) { cin >> l[i] >> r[i]; }
    vint start(4);
    vint goal(4);
    rep(i, h) rep(j, w) {
      if (l[i][j] == 'L') {
        start[0] = i;
        start[1] = j;
      }
      if (r[i][j] == 'R') {
        start[2] = i;
        start[3] = j;
      }
      if (l[i][j] == '%') {
        goal[0] = i;
        goal[1] = j;
      }
      if (r[i][j] == '%') {
        goal[2] = i;
        goal[3] = j;
      }
    }
    vi = 0;
    vs = 1;
    v[0][0] = start[0];
    v[0][1] = start[1];
    v[0][2] = start[2];
    v[0][3] = start[3];
    memset(used, 0, sizeof(used));
    used[start[0]][start[1]][start[2]][start[3]] = 1;
    while (vi < vs) {
      int *now = v[vi++];
      rep(i, 4) {
        int next[4];
        next[0] = now[0] + dy[i];
        next[1] = now[1] + dx[i];
        next[2] = now[2] + dy[i];
        next[3] = now[3] - dx[i];
        if (next[0] < 0 || h <= next[0] || l[next[0]][now[1]] == '#')
          next[0] = now[0];
        if (next[1] < 0 || w <= next[1] || l[now[0]][next[1]] == '#')
          next[1] = now[1];
        if (next[2] < 0 || h <= next[2] || r[next[2]][now[3]] == '#')
          next[2] = now[2];
        if (next[3] < 0 || w <= next[3] || r[now[2]][next[3]] == '#')
          next[3] = now[3];
        if (used[next[0]][next[1]][next[2]][next[3]] == 1)
          continue;
        used[next[0]][next[1]][next[2]][next[3]] = 1;
        if (l[next[0]][next[1]] == '%')
          continue;
        if (r[next[2]][next[3]] == '%')
          continue;
        rep(j, 4) v[vs][j] = next[j];
        ++vs;
      }
    }
    if (used[goal[0]][goal[1]][goal[2]][goal[3]])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}