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

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int h, w;
ll l;
char c[100][101];
int d[100][100][4];

int main() {
  while (scanf("%d%d%lld", &h, &w, &l), w) {
    rep(i, h) { scanf("%s", c[i]); }

    int p, q, r;
    rep(i, h) {
      rep(j, w) {
        if (c[i][j] == 'N') {
          r = 0;
        } else if (c[i][j] == 'E') {
          r = 1;
        } else if (c[i][j] == 'S') {
          r = 2;
        } else if (c[i][j] == 'W') {
          r = 3;
        } else {
          continue;
        }
        p = i;
        q = j;
        break;
      }
    }

    rep(i, h) {
      rep(j, w) { fill_n(d[i][j], 4, -1); }
    }
    d[p][q][r] = 0;

    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = {0, 1, 0, -1};
    while (true) {
      int v = d[p][q][r];
      int x = p + dx[r];
      int y = q + dy[r];
      if (0 <= x && x < h && 0 <= y && y < w && c[x][y] != '#') {
        p = x;
        q = y;
        ++v;
      } else {
        r = (r + 1) % 4;
      }

      if (v == l) {
        char k[5] = "NESW";
        printf("%d %d %c\\n", p + 1, q + 1, k[r]);
        break;
      }

      int a = d[p][q][r];
      if (a != -1) {
        l -= a;
        l %= (v - a);

        if (!l) {
          d[p][q][r] = v;

          int i = (r + 1) % 4;
          while (true) {
            if (d[p][q][i] == v) {
              r = i;
              break;
            }
            i = (i + 1) % 4;
          }

          char k[5] = "NESW";
          printf("%d %d %c\\n", p + 1, q + 1, k[r]);
          break;
        }

        rep(i, h) {
          rep(j, w) { fill_n(d[i][j], 4, -1); }
        }
        v = 0;
      }

      d[p][q][r] = v;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int h, w;
ll l;
char c[100][101];
int d[100][100][4];

int main() {
  while (scanf("%d%d%lld", &h, &w, &l), w) {
    rep(i, h) { scanf("%s", c[i]); }

    int p, q, r;
    rep(i, h) {
      rep(j, w) {
        if (c[i][j] == 'N') {
          r = 0;
        } else if (c[i][j] == 'E') {
          r = 1;
        } else if (c[i][j] == 'S') {
          r = 2;
        } else if (c[i][j] == 'W') {
          r = 3;
        } else {
          continue;
        }
        p = i;
        q = j;
        break;
      }
    }

    rep(i, h) {
      rep(j, w) { fill_n(d[i][j], 4, -1); }
    }
    d[p][q][r] = 0;

    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = {0, 1, 0, -1};
    while (true) {
      int v = d[p][q][r];
      int x = p + dx[r];
      int y = q + dy[r];
      if (0 <= x && x < h && 0 <= y && y < w && c[x][y] != '#') {
        p = x;
        q = y;
        ++v;
      } else {
        r = (r + 1) % 4;
      }

      if (v == l) {
        char k[5] = "NESW";
        printf("%d %d %c\\n", p + 1, q + 1, k[r]);
        break;
      }

      int a = d[p][q][r];
      if (a != -1) {
        l -= a;
        l %= (v - a);

        rep(i, h) {
          rep(j, w) { fill_n(d[i][j], 4, -1); }
        }
        d[p][q][r] = 0;
        continue;
      }

      d[p][q][r] = v;
    }
  }
  return 0;
}