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

#include <iostream>
#define N 105
using namespace std;
typedef long long ll;
char Rot(char);
int GetNum(char);

int main() {
  int h, w, y, x, r, me[N][N][4], ansy, ansx, flag;
  char c;
  string s[N];
  ll l, t;
  while (1) {
    cin >> h >> w >> l;
    if (!h && !w && !l)
      break;
    for (int i = 0; i < h; i++)
      cin >> s[i];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (s[i][j] != '.' && s[i][j] != '#')
          y = i, x = j, c = s[i][j];
        for (int k = 0; k < 4; k++)
          me[i][j][k] = -1;
      }
    t = 0;
    r = GetNum(c);
    me[y][x][r] = t;
    while (1) {
      int ny = y, nx = x;
      if (c == 'N')
        ny--;
      if (c == 'E')
        nx++;
      if (c == 'S')
        ny++;
      if (c == 'W')
        nx--;
      t++;
      flag = 0;
      if (ny < 0 || h <= ny || nx < 0 || w <= nx || s[ny][nx] == '#')
        flag = 1;
      if (t == l && !flag) {
        ansy = ny + 1, ansx = nx + 1;
        break;
      }
      if (!flag && me[ny][nx][r] != -1 && t - me[ny][nx][r] != 0) {
        t = l - ((l - me[ny][nx][r]) % (t - me[ny][nx][r]));
        if (l - t == 0) {
          ansy = ny + 1, ansx = nx + 1;
          break;
        }
      }
      if (flag)
        ny = y, nx = x, c = Rot(c), t--;
      y = ny, x = nx;
      r = GetNum(c);
      me[ny][nx][r] = t;
    }
    cout << ansy << ' ' << ansx << ' ' << c << endl;
  }
  return 0;
}

char Rot(char c) {
  if (c == 'N')
    return 'E';
  if (c == 'E')
    return 'S';
  if (c == 'S')
    return 'W';
  return 'N';
}

int GetNum(char c) {
  if (c == 'N')
    return 0;
  if (c == 'E')
    return 1;
  if (c == 'S')
    return 2;
  return 3;
}

Error: TLE

Fixed submission:

#include <iostream>
#define N 105
using namespace std;
typedef long long ll;
char Rot(char);
int GetNum(char);

int main() {
  ll h, w, y, x, t, r, me[N][N][4];
  char c;
  string s[N];

  ll l;
  while (1) {
    cin >> h >> w >> l;
    if (!h && !w && !l)
      break;
    for (int i = 0; i < h; i++)
      cin >> s[i];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (s[i][j] != '.' && s[i][j] != '#')
          y = i, x = j, c = s[i][j];
        for (int k = 0; k < 4; k++)
          me[i][j][k] = -1;
      }
    t = 0;
    r = GetNum(c);
    me[y][x][r] = t;
    while (1) {
      int ny = y, nx = x;
      if (c == 'N')
        ny--;
      if (c == 'E')
        nx++;
      if (c == 'S')
        ny++;
      if (c == 'W')
        nx--;
      if (ny < 0 || h <= ny || nx < 0 || w <= nx || s[ny][nx] == '#')
        ny = y, nx = x, c = Rot(c), t--;
      t++;
      y = ny, x = nx;
      if (l == t)
        break;
      r = GetNum(c);
      if (me[y][x][r] != -1 && t - me[y][x][r] != 0)
        t = l - ((l - me[y][x][r]) % (t - me[y][x][r]));
      me[ny][nx][r] = t;
    }
    cout << y + 1 << ' ' << x + 1 << ' ' << c << endl;
  }
  return 0;
}

char Rot(char c) {
  if (c == 'N')
    return 'E';
  if (c == 'E')
    return 'S';
  if (c == 'S')
    return 'W';
  return 'N';
}

int GetNum(char c) {
  if (c == 'N')
    return 0;
  if (c == 'E')
    return 1;
  if (c == 'S')
    return 2;
  return 3;
}