Problem p01437 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D; // ??§?¨?????????????double???long double?????????

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;
const int MOD = 1000000007;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int H, W;
ll L;

// h, w, dir
typedef tuple<int, int, int> State;

string directions = "NESW";
// NESW
int dh[] = {-1, 0, 1, 0};
int dw[] = {0, 1, 0, -1};

inline bool infield(int h, int w) { return 0 <= h && h < H && 0 <= w && w < W; }

void solve() {
  vector<string> field(H);
  for (int i = 0; i < H; i++)
    cin >> field[i];

  int dir = -1;
  int h = -1, w = -1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int tdir = directions.find(field[i][j]);
      if (tdir != string::npos) {
        dir = tdir;
        h = i;
        w = j;
        field[h][w] = '.';
      }
    }
  }

  map<State, ll> memo;

  while (L--) {
    while (!(infield(h + dh[dir], w + dw[dir]) &&
             field[h + dh[dir]][w + dw[dir]] == '.'))
      dir = (dir + 1) % 4;

    if (memo.find(State(h, w, dir)) != memo.end()) {
      ll d = memo[State(h, w, dir)];
      L %= (L - d);
    }
    memo[State(h, w, dir)] = L;

    h += dh[dir];
    w += dw[dir];
  }

  cout << h + 1 << " " << w + 1 << " " << directions[dir] << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout.precision(16);
  // cout.setf(ios::fixed);

  while (cin >> H >> W >> L, H | W | L) {
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D; // ??§?¨?????????????double???long double?????????

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;
const int MOD = 1000000007;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int H, W, L;

// h, w, dir
typedef tuple<int, int, int> State;

string directions = "NESW";
// NESW
int dh[] = {-1, 0, 1, 0};
int dw[] = {0, 1, 0, -1};

inline bool infield(int h, int w) { return 0 <= h && h < H && 0 <= w && w < W; }

void solve() {
  vector<string> field(H);
  for (int i = 0; i < H; i++)
    cin >> field[i];

  int dir = -1;
  int h = -1, w = -1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int tdir = directions.find(field[i][j]);
      if (tdir != string::npos) {
        dir = tdir;
        h = i;
        w = j;
        field[h][w] = '.';
      }
    }
  }

  map<State, int> memo;

  while (L--) {
    while (!(infield(h + dh[dir], w + dw[dir]) &&
             field[h + dh[dir]][w + dw[dir]] == '.'))
      dir = (dir + 1) % 4;

    if (memo.find(State(h, w, dir)) != memo.end()) {
      int d = memo[State(h, w, dir)];
      L %= (L - d);
    }
    memo[State(h, w, dir)] = L;

    h += dh[dir];
    w += dw[dir];
  }

  cout << h + 1 << " " << w + 1 << " " << directions[dir] << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout.precision(16);
  // cout.setf(ios::fixed);

  while (cin >> H >> W >> L, H | W | L) {
    solve();
  }

  return 0;
}