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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int field_cnt[100][100][4];
char field[100][100];

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
string ds = "NESW";

int main() {
  int h, w;
  ll n;
  int x, y, way;
  while (cin >> h >> w >> n, h | w | n) {
    fill_n((int *)field_cnt, 100 * 100 * 4, -1);
    fill_n((char *)field, 100 * 100, '#');
    int way = 0;
    REP(iy, h) {
      REP(ix, w) {
        cin >> field[iy][ix];
        char c = field[iy][ix];
        switch (c) {
        case 'W':
          way++;
        case 'S':
          way++;
        case 'E':
          way++;
        case 'N':
          x = ix;
          y = iy;
        }
      }
    }
    int cnt = 0;
    while (n > 0) {
      // LOG ("%d -- %d %d %c\\n",n,y,x,ds[way]);
      if (field_cnt[y][x][way] != -1) {
        if (n < cnt - field_cnt[y][x][way]) {
        } else {
          n %= cnt - field_cnt[y][x][way];
          if (n == 0)
            break;
        }
      }
      n--;
      field_cnt[y][x][way] = cnt;
      int nx = x + dx[way];
      int ny = y + dy[way];
      while (nx < 0 || ny < 0 || nx >= w || ny >= h || field[ny][nx] == '#') {
        way = (way + 1) % 4;
        nx = x + dx[way];
        ny = y + dy[way];
      }
      y = ny;
      x = nx;
      cnt++;
    }
    printf("%d %d %c\\n", y + 1, x + 1, ds[way]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int field_cnt[100][100][4];
char field[100][100];

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
string ds = "NESW";

int main() {
  int h, w, n;
  int x, y, way;
  while (cin >> h >> w >> n, h | w | n) {
    fill_n((int *)field_cnt, 100 * 100 * 4, -1);
    int way = 0;
    REP(iy, h) {
      REP(ix, w) {
        cin >> field[iy][ix];
        char c = field[iy][ix];
        switch (c) {
        case 'W':
          way++;
        case 'S':
          way++;
        case 'E':
          way++;
        case 'N':
          x = ix;
          y = iy;
        }
      }
    }
    int cnt = 0;
    while (n > 0) {
      // LOG ("%d -- %d %d %c\\n",n,y,x,ds[way]);
      if (field_cnt[y][x][way] != -1) {
        if (n < cnt - field_cnt[y][x][way]) {
        } else {
          n %= cnt - field_cnt[y][x][way];
          if (n == 0)
            break;
          n++;
        }
      }
      n--;
      field_cnt[y][x][way] = cnt;
      int nx = x + dx[way];
      int ny = y + dy[way];
      while (nx < 0 || ny < 0 || nx >= w || ny >= h || field[ny][nx] == '#') {
        way = (way + 1) % 4;
        nx = x + dx[way];
        ny = y + dy[way];
      }
      y = ny;
      x = nx;
      cnt++;
    }
    printf("%d %d %c\\n", y + 1, x + 1, ds[way]);
  }
}