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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int H, W;
ll L;
char c[128][128];
int dist[4][128][128]; // N E S W
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};

int convert(char ch) {
  return ch == 'N' ? 0 : ch == 'E' ? 1 : ch == 'S' ? 2 : 3;
}
char reconvert(int n) {
  return n == 0 ? 'N' : n == 1 ? 'E' : n == 2 ? 'S' : 'W';
}

void solve(int sy, int sx, int sd) {
  int y = sy, x = sx, dir = sd;
  dist[dir][y][x] = 0;
  for (int step = 1;; step++) {
    int nd = dir;
    int ny = y + dy[nd], nx = x + dx[nd];
    while (ny < 0 || ny >= H || nx < 0 || nx >= W || c[ny][nx] == '#') {
      nd = (nd + 1) % 4;
      ny = y + dy[nd], nx = x + dx[nd];
    }

    if (dist[nd][ny][nx] != -1) {
      ll l = L - dist[nd][ny][nx];
      l %= (dist[dir][y][x] - dist[nd][ny][nx] + 1);
      rep(i, 4) rep(j, 128) rep(k, 128) {
        if (dist[i][j][k] != l + dist[nd][ny][nx])
          continue;
        printf("%d %d %c\\n", j + 1, k + 1, reconvert(i));
        return;
      }
    }
    y = ny, x = nx, dir = nd;
    dist[dir][y][x] = step;
    if (step == L) {
      printf("%d %d %c\\n", y + 1, x + 1, reconvert(dir));
      return;
    }
  }
}

int main() {
  while (scanf("%d%d%lld", &H, &W, &L) && H + W + L) {
    int sy, sx, sd;
    memset(dist, -1, sizeof(dist));
    rep(i, H) {
      scanf("%s", c[i]);
      rep(j, W) {
        if (c[i][j] == '#' || c[i][j] == '.')
          continue;
        sy = i;
        sx = j;
        sd = convert(c[i][j]);
        c[i][j] = '.';
      }
    }

    // int y = sy, x = sx, dir = sd;
    //     for(int step=0;;step++){
    //     }
    solve(sy, sx, sd);
    /*    rep(i, 4){
      rep(j, H){
        rep(k, W) printf("%d ", dist[i][j][k]); puts("");
      }
      puts("");
      }*/
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int H, W;
ll L;
char c[128][128];
int dist[4][128][128]; // N E S W
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};

int convert(char ch) {
  return ch == 'N' ? 0 : ch == 'E' ? 1 : ch == 'S' ? 2 : 3;
}
char reconvert(int n) {
  return n == 0 ? 'N' : n == 1 ? 'E' : n == 2 ? 'S' : 'W';
}

void solve(int sy, int sx, int sd) {
  int y = sy, x = sx, dir = sd;
  dist[dir][y][x] = 0;
  for (int step = 1;; step++) {
    int nd = dir;
    int ny = y + dy[nd], nx = x + dx[nd];
    while (ny < 0 || ny >= H || nx < 0 || nx >= W || c[ny][nx] == '#') {
      nd = (nd + 1) % 4;
      ny = y + dy[nd], nx = x + dx[nd];
    }

    if (dist[nd][ny][nx] != -1) {
      ll l = L - dist[nd][ny][nx];
      l %= (dist[dir][y][x] - dist[nd][ny][nx] + 1);
      rep(i, 4) rep(j, 128) rep(k, 128) {
        if (dist[i][j][k] != l + dist[nd][ny][nx])
          continue;
        printf("%d %d %c\\n", j + 1, k + 1, reconvert(i));
        return;
      }
    }
    y = ny, x = nx, dir = nd;
    if (step == L) {
      printf("%d %d %c\\n", y + 1, x + 1, reconvert(dir));
      return;
    }
  }
}

int main() {
  while (scanf("%d%d%lld", &H, &W, &L) && H + W + L) {
    int sy, sx, sd;
    memset(dist, -1, sizeof(dist));
    rep(i, H) {
      scanf("%s", c[i]);
      rep(j, W) {
        if (c[i][j] == '#' || c[i][j] == '.')
          continue;
        sy = i;
        sx = j;
        sd = convert(c[i][j]);
        c[i][j] = '.';
      }
    }

    // int y = sy, x = sx, dir = sd;
    //     for(int step=0;;step++){
    //     }
    solve(sy, sx, sd);
  }
  return 0;
}