Problem p01437 (C++)

Buggy submission (status: Time Limit Exceeded):
#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 <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#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 FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
char dd[] = {'N', 'E', 'S', 'W'};

typedef vector<ll> vl;
typedef vector<vl> vvl;

int main() {
  ll H, W, L;
  while (cin >> H >> W >> L, H) {
    vs field(H);
    REP(i, H) { cin >> field[i]; }

    int cx, cy, cd;
    REP(i, H) {
      REP(j, W) {
        if (field[i][j] != '.' && field[i][j] != '#') {
          cx = j;
          cy = i;
          switch (field[i][j]) {
          case 'N':
            cd = 0;
            break;
          case 'E':
            cd = 1;
            break;
          case 'S':
            cd = 2;
            break;
          case 'W':
            cd = 3;
            break;
          }
          break;
        }
      }
    }

    vector<vvl> turn(H, vvl(W, vl(4)));
    bool looped = false;
    for (ll t = 0; t < L; t++) {
      if (!looped && turn[cy][cx][cd] != 0) {
        looped = true;
        ll cycle = (t - turn[cy][cx][cd] - 1);
        t += (L - t) / cycle;
      }

      int nx = cx + dx[cd];
      int ny = cy + dy[cd];

      if (ny < 0 || H <= ny || nx < 0 || W <= nx || field[ny][nx] == '#') {
        cd = (cd + 1) % 4;
        t--;
        continue;
      }

      turn[cy][cx][cd] = t;
      cy = ny;
      cx = nx;
    }

    cout << cy + 1 << " " << cx + 1 << " " << dd[cd] << endl;
  }

  return 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 <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#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 FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
char dd[] = {'N', 'E', 'S', 'W'};

typedef vector<ll> vl;
typedef vector<vl> vvl;

int main() {
  ll H, W, L;
  while (cin >> H >> W >> L, H) {
    vs field(H);
    REP(i, H) { cin >> field[i]; }

    int cx, cy, cd;
    REP(i, H) {
      REP(j, W) {
        if (field[i][j] != '.' && field[i][j] != '#') {
          cx = j;
          cy = i;
          switch (field[i][j]) {
          case 'N':
            cd = 0;
            break;
          case 'E':
            cd = 1;
            break;
          case 'S':
            cd = 2;
            break;
          case 'W':
            cd = 3;
            break;
          }
          break;
        }
      }
    }

    vector<vvl> turn(H, vvl(W, vl(4)));
    bool looped = false;
    for (ll t = 0; t < L; t++) {
      if (!looped && turn[cy][cx][cd] != 0) {
        looped = true;
        ll cycle = (t - turn[cy][cx][cd]);
        t += ((L - t) / cycle) * cycle;
        if (t == L) {
          t -= cycle;
        }
      }

      int nx = cx + dx[cd];
      int ny = cy + dy[cd];

      if (ny < 0 || H <= ny || nx < 0 || W <= nx || field[ny][nx] == '#') {
        cd = (cd + 1) % 4;
        t--;
        continue;
      }

      turn[cy][cx][cd] = t;
      cy = ny;
      cx = nx;
    }

    cout << cy + 1 << " " << cx + 1 << " " << dd[cd] << endl;
  }

  return 0;
}