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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int n, t;
int sx, sy;
bool mas[55][55][55][55];
map<int, int> mp;
int D;

bool used[4][55][55][1010];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
string ds = "NESW";
using T4 = tuple<int, int, int, int>;
bool in(int x, int y) { return 0 <= x && x <= 50 && 0 <= y && y <= 50; }
void bfs() {
  set<Pi> ans;
  queue<T4> que;
  memset(used, false, sizeof(used));
  rep(i, 4) {
    int nx = sx + dx[i], ny = sy + dy[i];
    if (!in(nx, ny) || !mas[sx][sy][nx][ny])
      continue;
    que.emplace(nx, ny, i, 1);
    used[i][ny][nx][1] = true;
  }
  while (!que.empty()) {
    int x, y, dir, dist;
    tie(x, y, dir, dist) = que.front();
    que.pop();
    // cout<<x<<" "<<y<<" "<<dir<<" "<<dist<<endl;
    if (dist > D)
      break;
    rep(i, 4) {
      if (i == (dir + 2) % 4)
        continue;
      int nx = x + dx[i], ny = y + dy[i];
      if (!in(nx, ny) || !mas[x][y][nx][ny])
        continue;
      if (mp.count(dist)) {
        if (dir != mp[dist] && i != mp[dist])
          continue;
        if (used[i][ny][nx][dist + 1])
          continue;
        que.emplace(nx, ny, i, dist + 1);
        used[i][ny][nx][dist + 1] = true;
        if (dist == D)
          ans.emplace(x, y);
      } else {
        if (used[i][ny][nx][dist + 1])
          continue;
        que.emplace(nx, ny, i, dist + 1);
        used[i][ny][nx][dist + 1] = true;
      }
    }
  }
  for (Pi p : ans)
    cout << p.first << " " << p.second << endl;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  memset(mas, false, sizeof(mas));

  cin >> n >> sx >> sy >> t;
  rep(i, n) {
    int ax, ay, bx, by;
    cin >> ax >> ay >> bx >> by;
    if (ax == bx) {
      if (ay > by)
        swap(ay, by);
      reps(y, ay, by) mas[ax][y][ax][y + 1] = mas[ax][y + 1][ax][y] = true;
    } else {
      if (ax > bx)
        swap(ax, bx);
      reps(x, ax, bx) mas[x][ay][x + 1][ay] = mas[x + 1][ay][x][ay] = true;
    }
  }
  D = 0;
  rep(i, t) {
    int d;
    char c;
    cin >> d >> c;
    D += d;
    mp[D] = ds.find(c);
  }

  bfs();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int n, t;
int sx, sy;
bool mas[55][55][55][55];
map<int, int> mp;
int D;

bool used[4][55][55][1010];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
string ds = "NESW";
using T4 = tuple<int, int, int, int>;
bool in(int x, int y) { return 0 <= x && x <= 50 && 0 <= y && y <= 50; }
void bfs() {
  set<Pi> ans;
  queue<T4> que;
  memset(used, false, sizeof(used));
  rep(i, 4) {
    int nx = sx + dx[i], ny = sy + dy[i];
    if (!in(nx, ny) || !mas[sx][sy][nx][ny])
      continue;
    que.emplace(nx, ny, i, 1);
    used[i][ny][nx][1] = true;
  }
  while (!que.empty()) {
    int x, y, dir, dist;
    tie(x, y, dir, dist) = que.front();
    que.pop();
    // cout << x <<" "<<y<<" "<<dir<<" "<<dist<<endl;
    // if(dist == D+1) ans.emplace(x, y);
    if (dist > D)
      break;
    rep(i, 4) {
      if (i == (dir + 2) % 4)
        continue;
      int nx = x + dx[i], ny = y + dy[i];
      if (!in(nx, ny) || !mas[x][y][nx][ny])
        continue;
      // cout<<"FOO"<<endl;
      if (mp.count(dist)) {
        // cout<<"HOGE"<<endl;
        if (dir != mp[dist] && i != mp[dist])
          continue;
        // cout<<"PIYO"<<endl;
        if (used[i][ny][nx][dist + 1])
          continue;
        que.emplace(nx, ny, i, dist + 1);
        if (dist == D)
          ans.emplace(x, y);
      } else {
        // cout<<dist<<endl;
        if (used[i][ny][nx][dist + 1])
          continue;
        que.emplace(nx, ny, i, dist + 1);
      }
    }
  }
  for (Pi p : ans)
    cout << p.first << " " << p.second << endl;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  memset(mas, false, sizeof(mas));

  cin >> n >> sx >> sy >> t;
  rep(i, n) {
    int ax, ay, bx, by;
    cin >> ax >> ay >> bx >> by;
    if (ax == bx) {
      if (ay > by)
        swap(ay, by);
      reps(y, ay, by) mas[ax][y][ax][y + 1] = mas[ax][y + 1][ax][y] = true;
    } else {
      if (ax > bx)
        swap(ax, bx);
      reps(x, ax, bx) mas[x][ay][x + 1][ay] = mas[x + 1][ay][x][ay] = true;
    }
  }
  D = 0;
  rep(i, t) {
    int d;
    char c;
    cin >> d >> c;
    D += d;
    mp[D] = ds.find(c);
  }

  bfs();

  return 0;
}