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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <strstream>
#include <vector>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef long long ll;

int x = 10, y = 10;

int measure(char c, int m, bool jewel[][21], int flag) {
  if (c == 'N') {
    rep(i, m) {
      y++;
      if (jewel[x][y]) {
        flag++;
        jewel[x][y] = false;
      }
    }
  } else if (c == 'E') {
    rep(i, m) {
      x++;
      if (jewel[x][y]) {
        flag++;
        jewel[x][y] = false;
      }
    }
  } else if (c == 'S') {
    rep(i, m) {
      y--;
      if (jewel[x][y]) {
        flag++;
        jewel[x][y] = false;
      }
    }
  } else if (c == 'W') {
    rep(i, m) {
      x--;
      if (jewel[x][y]) {
        flag++;
        jewel[x][y] = false;
      }
    }
  }
  return flag;
}

int main() {
  int n, m;

  while (cin >> n, n) {
    x = 10, y = 10;
    bool jewel[21][21] = {0};
    int flag = 0;

    rep(i, n) {
      int x, y;
      cin >> x >> y;
      jewel[x][y] = true;
    }

    cin >> m;

    rep(i, m) {
      char compass;
      int di;
      cin >> compass >> di;
      flag += measure(compass, di, jewel, 0);
    }
    if (n == flag)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
using namespace std;

int dfs(vector<pair<int, int>>::iterator vmass_it,
        vector<pair<char, int>>::iterator robo_it, int n, int m) {
  pair<int, int> mass = make_pair(10, 10); // 現在地
  int ans = 0;                             // ans == n (ture)
  for (int i = 0; i < m; robo_it++) {
    vmass_it -= n;
    // Y軸
    if (robo_it->first == 'N') {
      mass.second += robo_it->second;
    } else if (robo_it->first == 'S') {
      mass.second += robo_it->second;
    }

    // X軸
    else if (robo_it->first == 'E') {
      mass.first += robo_it->second;
    } else // W
    {
      mass.first += robo_it->second;
    }
    for (int j = 0; j < n; vmass_it++) {
      if (vmass_it->first == mass.first && vmass_it->second == mass.second) {
        ans++;
        vmass_it->first = 30;
      }
    }
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<pair<int, int>> mass(n);
    rep(i, n) {
      pair<int, int> p;
      cin >> p.first >> p.second;
      mass[i] = p;
    }
    int m;
    cin >> m;
    vector<pair<char, int>> robo(m);
    rep(i, m) {
      pair<char, int> p;
      cin >> p.first >> p.second;
      robo[i] = p;
    }
    if (dfs(mass.begin(), robo.begin(), n, m) == n)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}