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

#include <stdio.h>
#include <string.h>
#include <vector>
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
struct S {
  int x, y, a;
  S(int x, int y, int a) : x(x), y(y), a(a) {}
};
int F(char c) { return c == '.' || c == 'X'; }
int main() {
  char *p, a[32][32];
  int W, H, x, y, t, i, j;
  while (scanf("%d%d", &W, &H), W) {
    std::vector<S> v, u[4], g, b;
    for (y = 0; y < H; ++y) {
      scanf("%s", a[y]);
      for (p = a[y]; p = strpbrk(p, "NEWS"); ++p)
        v.push_back(S(p - a[y], y,
                      *p == 'W'   ? 0
                      : *p == 'S' ? 1
                      : *p == 'E' ? 2
                                  : 3));
    }
    for (t = 0; t < 181 && v.size(); ++t) {
      for (i = 0; i < 4; ++i)
        u[i].clear();
      b.clear();
      for (i = 0; i < v.size(); ++i) {
        S s = v[i];
        for (j = 0; j < 4; ++j) {
          x = s.x + dx[(s.a + 3 + j) % 4];
          y = s.y + dy[(s.a + 3 + j) % 4];
          if (F(a[y][x])) {
            s.a = (s.a + 3 + j) % 4;
            u[s.a].push_back(s);
            goto N;
          }
        }
        b.push_back(s);
      N:;
      }
      v = b;
      g.clear();
      for (i = 0; i < 4; ++i) {
        for (j = 0; j < u[i].size(); ++j) {
          S s = u[i][j];
          x = s.x + dx[i];
          y = s.y + dy[i];
          if (F(a[y][x])) {
            a[s.y][s.x] = '.';
            s.x = x;
            s.y = y;
            if (a[y][x] == 'X') {
              g.push_back(s);
              a[y][x] = '@';
              continue;
            }
            a[y][x] = '@';
          }
          v.push_back(s);
        }
      }
      for (i = 0; i < g.size(); ++i)
        a[g[i].y][g[i].x] = 'X';
    }
    if (t < 181)
      printf("%d\\n", t);
    else
      puts("NA");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>
#include <vector>
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
struct S {
  int x, y, a;
  S() {}
  S(int x, int y, int a) : x(x), y(y), a(a) {}
};
int F(char c) { return c == '.' || c == 'X'; }
int main() {
  char *p, a[32][32];
  int W, H, x, y, t, i, j;
  while (scanf("%d%d", &W, &H), W) {
    std::vector<S> v, u[4];
    for (y = 0; y < H; ++y) {
      scanf("%s", a[y]);
      for (p = a[y]; p = strpbrk(p, "NEWS"); ++p)
        v.push_back(S(p - a[y], y,
                      *p == 'W'   ? 0
                      : *p == 'S' ? 1
                      : *p == 'E' ? 2
                                  : 3));
    }
    for (t = 0; t < 181 && v.size(); ++t) {
      for (i = 0; i < 4; ++i)
        u[i].clear();
      for (i = 0; i < v.size(); ++i) {
        S s = v[i];
        for (j = 0; j < 4; ++j) {
          x = s.x + dx[(s.a + 3 + j) & 3];
          y = s.y + dy[(s.a + 3 + j) & 3];
          if (F(a[y][x])) {
            s.a = (s.a + 3 + j) & 3;
            u[s.a].push_back(s);
            goto N;
          }
        }
        v.push_back(s);
      N:;
      }
      v.clear();
      for (i = 0; i < 4; ++i) {
        for (j = 0; j < u[i].size(); ++j) {
          S s = u[i][j];
          x = s.x + dx[i];
          y = s.y + dy[i];
          if (F(a[y][x])) {
            a[s.y][s.x] = '.';
            if (a[y][x] == 'X')
              continue;
            a[y][x] = '@';
            s.x = x;
            s.y = y;
          }
          v.push_back(s);
        }
      }
    }
    if (t < 181)
      printf("%d\\n", t);
    else
      puts("NA");
  }
  return 0;
}