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

#include <iostream>
#include <vector>

using namespace std;

int getdir(char c) {
  switch (c) {
  case 'N':
    return 0;
  case 'E':
    return 1;
  case 'S':
    return 2;
  case 'W':
    return 3;
  default:
    return -1;
  }
}

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

int main(void) {
  int h, w;
  long long l;
  while (cin >> h >> w >> l, h | w | l) {
    vector<string> vs;
    long long count = 0;
    string wall = "";
    int memo[101][101][4];
    fill_n((int *)memo, 101 * 101 * 4, -1);

    for (int i = 0; i < w + 2; i++) {
      wall += "#";
    }
    vs.push_back(wall);
    string input;
    for (int i = 0; i < h; i++) {
      cin >> input;
      vs.push_back("#" + input + "#");
    }
    vs.push_back(wall);

    int x, y, dir;
    for (int j = 0; j < h + 2; j++) {
      for (int i = 0; i < w + 2; i++) {
        if (vs[j][i] != '#' && vs[j][i] != '.') {
          x = i;
          y = j;
          dir = getdir(vs[j][i]);
          vs[j][i] = '.';
        }
      }
    }

    memo[y][x][dir] = 0;
    while (l > 0) {
      if (vs[y + dy[dir]][x + dx[dir]] == '.') {
        y += dy[dir];
        x += dx[dir];
        l--;
        count++;
      } else {
        dir = (dir + 1) % 4;
        continue;
      }
      if (memo[y][x][dir] != -1) {
        l = l % (count - memo[y][x][dir]);
      }
      memo[y][x][dir] = count;
    }
    cout << y << " " << x << " " << num2dir[dir] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int getdir(char c) {
  switch (c) {
  case 'N':
    return 0;
  case 'E':
    return 1;
  case 'S':
    return 2;
  case 'W':
    return 3;
  default:
    return -1;
  }
}

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

int main(void) {
  int h, w;
  long long l;
  while (cin >> h >> w >> l, h | w | l) {
    vector<string> vs;
    int count = 0;
    string wall = "";
    bool used[101][101];
    fill_n((bool *)used, 101 * 101, true);
    for (int i = 0; i < w + 2; i++) {
      wall += "#";
    }
    vs.push_back(wall);
    string input;
    for (int i = 0; i < h; i++) {
      cin >> input;
      vs.push_back("#" + input + "#");
    }
    vs.push_back(wall);

    int x, y, dir;
    for (int j = 0; j < h + 2; j++) {
      for (int i = 0; i < w + 2; i++) {
        if (vs[j][i] != '#' && vs[j][i] != '.') {
          x = i;
          y = j;
          dir = getdir(vs[j][i]);
          vs[j][i] = '.';
        }
        if (vs[j][i] == '.') {
          used[j][i] = false;
        }
      }
    }

    int sx, sy, sdir;
    sx = x;
    sy = y;
    sdir = dir;
    while (l > 0) {
      if (vs[y + dy[dir]][x + dx[dir]] == '.') {
        y += dy[dir];
        x += dx[dir];
        l--;
        count++;
        used[y][x] = true;
        int allused = true;
        for (int j = 0; j < h + 2; j++) {
          for (int i = 0; i < w + 2; i++) {
            allused &= used[j][i];
          }
        }
        if (allused) {
          l = l / count + l % count;
        }
      } else
        dir = (dir + 1) % 4;
    }
    cout << y << " " << x << " " << num2dir[dir] << endl;
  }
  return 0;
}