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

#include <iostream>
#include <vector>
using namespace std;

const string Direction = {"ENWS"};
const int Next[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
enum { Y, X };

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    int time;
    vector<string> field(h);
    for (int i = 0; i < h; ++i)
      cin >> field[i];

    for (time = 0; time <= 180; ++time) {
      //             cout << time << endl;
      //             for(auto& s: field)cout<<s<<endl;
      bool ok = true;
      for (const auto &line : field)
        for (const auto &c : line)
          if (Direction.find(c) != string::npos)
            ok = false;
      if (ok)
        break;
      // turn
      for (int i = 0; i < h; ++i)
        for (int j = 0; j < w; ++j) {
          int index = Direction.find(field[i][j]);
          if (index == string::npos)
            continue;
          for (int k = 0; k < 4; ++k) {
            int d = (index + 3 + k) % 4;
            int y = i + Next[d][Y];
            int x = j + Next[d][X];
            if (field[y][x] != '.' && field[y][x] != 'X')
              continue;
            field[i][j] = Direction[d];
            break;
          }
        }
      // move
      vector<vector<int>> used(h, vector<int>(w, 0));
      for (int i = 0; i < h; ++i)
        for (int j = 0; j < w; ++j) {
          if (field[i][j] != '.' && field[i][j] != 'X')
            continue;
          if (used[i][j])
            continue;
          for (int d = 0; d < 4; ++d) {
            int y = i + Next[d][Y];
            int x = j + Next[d][X];
            if (y < 0 || h <= y)
              continue;
            if (x < 0 || w <= x)
              continue;
            if (field[y][x] != Direction[(d + 2) % 4])
              continue;
            if (used[y][x])
              continue;
            used[i][j] = 1;
            used[y][x] = 1;
            if (field[i][j] == '.')
              field[i][j] = field[y][x];
            field[y][x] = '.';
            break;
          }
        }
    }

    if (time > 180)
      cout << "NA" << endl;
    else
      cout << time << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const string Direction = {"ENWS"};
const int Next[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    int time;
    vector<string> map(h);
    for (int i = 0; i < h; ++i)
      cin >> map[i];

    for (time = 0; time <= 180; ++time) {
      bool ok = true;
      for (const auto &line : map)
        for (const auto &c : line)
          if (Direction.find(c) != string::npos)
            ok = false;
      if (ok)
        break;
      // turn
      for (int i = 0; i < h; ++i)
        for (int j = 0; j < w; ++j) {
          int index = Direction.find(map[i][j]);
          if (index == string::npos)
            continue;
          index = (index - 1 + 4) % 4;
          for (int k = 0; k < 5; ++k) {
            int y = i + Next[index][0];
            int x = j + Next[index][1];
            if (map[y][x] == '.' || map[y][x] == 'X')
              break;
            index = (index + 1) % 4;
          }
          map[i][j] = Direction[index];
        }
      // move
      vector<vector<int>> used(h, vector<int>(w, 0));
      for (int i = 0; i < h; ++i)
        for (int j = 0; j < w; ++j) {
          if (map[i][j] != '.' && map[i][j] != 'X')
            continue;
          for (int d = 0; d < 4; ++d) {
            int y = i + Next[d][0];
            int x = j + Next[d][1];
            int index = Direction.find(map[y][x]);
            if (index == string::npos)
              continue;
            if ((index - d + 4) % 4 != 2)
              continue;
            if (used[y][x])
              continue;
            used[i][j] = 1;
            if (map[i][j] == '.')
              swap(map[i][j], map[y][x]);
            else
              map[y][x] = '.';
            break;
          }
        }
    }

    if (time > 180)
      cout << "NA" << endl;
    else
      cout << time << endl;
  }
}