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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define EXIST2(s, e) (find(ALL(s), (e)) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
const int MAX = INT_MAX / 10;
struct node {
  pii p;
  int dir;
  node(pii p, int dir) : p(p), dir(dir) {}
  bool operator==(const node &other) const {
    return other.p == p && other.dir == dir;
  }
};
char dirs[] = {'N', 'E', 'S', 'W'};
int main() {
  int h, w;
  ll s;
  while (cin >> h >> w >> s, h || w || s) {
    vs f(h);
    pii start;
    int initial_dir;
    REP(i, h) {
      cin >> f[i];
      REP(j, w) {
        switch (f[i][j]) {
        case 'N':
          initial_dir = 0;
          start = make_pair(i, j);
          break;
        case 'S':
          initial_dir = 2;
          start = make_pair(i, j);
          break;
        case 'E':
          initial_dir = 1;
          start = make_pair(i, j);
          break;
        case 'W':
          initial_dir = 3;
          start = make_pair(i, j);
          break;
        }
      }
    }
    vector<node> nodes;
    set<node> visited;
    pii cur = start;
    int dir = initial_dir;
    int begin = 0;
    while ((nodes.size() == 0 || cur.first != start.first ||
            cur.second != start.second || dir != initial_dir) &&
           nodes.size() < s) {
      int y;
      int x;
      while (y = cur.first + dy[dir], x = cur.second + dx[dir],
             y < 0 || x < 0 || y >= h || x >= w || f[y][x] == '#') {
        dir = (dir + 1) % 4;
      }
      cur.first = y;
      cur.second = x;
      node n(cur, dir);
      vector<node>::iterator it = find(ALL(nodes), n);
      if (it != nodes.end()) {
        begin = it - nodes.begin();
        break;
      }
      nodes.push_back(n);
    }
    int index = begin + (s - begin - 1) % (nodes.size() - begin);
    node &n = nodes[index];
    cout << n.p.first + 1 << " " << n.p.second + 1 << " " << dirs[n.dir]
         << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define EXIST2(s, e) (find(ALL(s), (e)) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
const int MAX = INT_MAX / 10;
struct node {
  pii p;
  int dir;
  node(pii p, int dir) : p(p), dir(dir) {}
};
char dirs[] = {'N', 'E', 'S', 'W'};
int main() {
  int h, w;
  ll s;
  while (cin >> h >> w >> s, h || w || s) {
    vs f(h);
    pii start;
    int initial_dir;
    REP(i, h) {
      cin >> f[i];
      REP(j, w) {
        switch (f[i][j]) {
        case 'N':
          initial_dir = 0;
          start = make_pair(i, j);
          break;
        case 'S':
          initial_dir = 2;
          start = make_pair(i, j);
          break;
        case 'E':
          initial_dir = 1;
          start = make_pair(i, j);
          break;
        case 'W':
          initial_dir = 3;
          start = make_pair(i, j);
          break;
        }
      }
    }
    vector<node> nodes;
    pii cur = start;
    int dir = initial_dir;
    while ((nodes.size() == 0 || cur.first != start.first ||
            cur.second != start.second || dir != initial_dir) &&
           nodes.size() < s) {
      int y;
      int x;
      while (y = cur.first + dy[dir], x = cur.second + dx[dir],
             y < 0 || x < 0 || y >= h || x >= w || f[y][x] == '#') {
        dir = (dir + 1) % 4;
      }
      cur.first = y;
      cur.second = x;
      nodes.push_back(node(cur, dir));
    }
    int remain = (s - 1) % nodes.size();
    node &n = nodes[remain];
    cout << n.p.first + 1 << " " << n.p.second + 1 << " " << dirs[n.dir]
         << endl;
  }
}