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

#include <iostream>
#include <map>
#include <set>
using namespace std;

typedef pair<int, int> P;

int dir(char a) {
  if (a == 'E')
    return 0;
  if (a == 'N')
    return 1;
  if (a == 'W')
    return 2;
  if (a == 'S')
    return 3;
  return -1;
}

char inv_dir(int a) {
  if (a == 0)
    return 'E';
  if (a == 1)
    return 'N';
  if (a == 2)
    return 'W';
  return 'S';
}

int main() {
  int h, w;
  string grid[30];
  bool use[30][30];
  int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

  while (cin >> w >> h && (w || h)) {
    for (int i = 0; i < h; i++)
      cin >> grid[i];

    int ans = 0;
    while (ans <= 180) {

      int num = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          int d = dir(grid[i][j]);
          if (0 <= d) {
            num++;
            for (int k = -1; k < 3; k++) {
              int dd = (d + k + 4) % 4;
              int sy = i + dy[dd], sx = j + dx[dd];
              if (sy < 0 || sx < 0 || h <= sy || w <= sx)
                continue;
              if (grid[sy][sx] == '.' || grid[sy][sx] == 'X') {
                grid[i][j] = inv_dir(dd);
                break;
              }
            }
          }
        }
      }
      if (!num)
        break;
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          use[i][j] = false;

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (!use[i][j] && (grid[i][j] == '.' || grid[i][j] == 'X')) {
            for (int k = 0; k < 4; k++) {
              int sy = i + dy[k], sx = j + dx[k];
              if (sy < 0 || sx < 0 || h <= sy || w <= sx || use[sy][sx])
                continue;
              int d = dir(grid[sy][sx]);
              if (d < 0)
                continue;
              int dd = (k + 2) % 4;
              if (d == dd) {
                grid[sy][sx] = '.';
                if (grid[i][j] != 'X')
                  grid[i][j] = inv_dir(d);
                use[i][j] = true;
                use[sy][sx] = true;
                break;
              }
            }
          }
        }
      }
      ans++;
      /*
      if(ans<10){
        cout << ans << endl;
        for(int i=0;i<h;i++)cout << grid[i] << endl;
        cout << endl;
      }
      */
    }
    if (ans > 180)
      cout << "NA\\n";
    else
      cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <set>
using namespace std;

typedef pair<int, int> P;

int dir(char a) {
  if (a == 'E')
    return 0;
  if (a == 'N')
    return 1;
  if (a == 'W')
    return 2;
  if (a == 'S')
    return 3;
  return -1;
}

char inv_dir(int a) {
  if (a == 0)
    return 'E';
  if (a == 1)
    return 'N';
  if (a == 2)
    return 'W';
  return 'S';
}

int main() {
  int h, w;
  string grid[100];
  int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

  while (cin >> w >> h && (w || h)) {
    for (int i = 0; i < h; i++)
      cin >> grid[i];

    int ans = 0;
    while (ans <= 180) {
      set<P> s;
      int num = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          int d = dir(grid[i][j]);
          if (0 <= d) {
            num++;
            for (int k = -1; k < 3; k++) {
              int dd = (d + k + 4) % 4;
              int sy = i + dy[dd], sx = j + dx[dd];
              if (sy < 0 || sx < 0 || h <= sy || w <= sx)
                continue;
              if (grid[sy][sx] == '.' || grid[sy][sx] == 'X') {
                grid[i][j] = inv_dir(dd);
                s.insert(P(sy, sx));
                break;
              }
            }
          }
        }
      }
      if (!num)
        break;
      set<P>::iterator ite;
      for (ite = s.begin(); ite != s.end(); ite++) {
        P p = *ite;
        for (int i = 0; i < 4; i++) {
          int sy = p.first + dy[i], sx = p.second + dx[i];
          // if(sy<0 || sx<0 || h<=sy || w<=sx)continue;
          int dd = (i + 2) % 4;
          if (dir(grid[sy][sx]) == dd) {
            grid[sy][sx] = '.';
            if (grid[p.first][p.second] != 'X')
              grid[p.first][p.second] = inv_dir(dd);
            break;
          }
        }
      }
      ans++;
      /*
      if(ans<10){
        cout << ans << endl;
        for(int i=0;i<h;i++)cout << grid[i] << endl;
        cout << endl;
      }
      */
    }
    if (ans > 180)
      cout << "NA\\n";
    else
      cout << ans << endl;
  }
}