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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
typedef long long int ll;

int main() {
  while (1) {
    int H, W;
    ll L;
    char C[100][100];

    cin >> H >> W >> L;
    if (H == 0)
      return 0;

    int sx, sy, sd;

    rep(y, 0, H) rep(x, 0, W) {
      char c;
      cin >> c;
      if (c != '.' && c != '#') {
        sx = x;
        sy = y;
        switch (c) {
        case 'N':
          sd = 0;
          break;
        case 'E':
          sd = 1;
          break;
        case 'S':
          sd = 2;
          break;
        case 'W':
          sd = 3;
          break;
        }
        C[y][x] = '.';
      } else
        C[y][x] = c;
    }

    ll memo[100][100][4];
    rep(y, 0, H) rep(x, 0, W) rep(i, 0, 4) memo[y][x][i] = -1;
    bool noloop = true;

    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {-1, 0, 1, 0};

    for (ll l = 0; l < L; l++) {
      rep(i, 0, 4) {
        int d = (sd + i) % 4;
        if (noloop) {
          if (memo[sy][sx][d] < 0) {
            memo[sy][sx][d] = l;
          } else {
            int dl = l - memo[sy][sx][d];
            l = l + ((L - 1 - l) / dl) * dl;
            noloop = false;
          }
        }
        int x = sx + dx[d];
        int y = sy + dy[d];
        if (x < 0 || W <= x)
          continue;
        if (y < 0 || H <= y)
          continue;
        if (C[y][x] == '#')
          continue;

        sx = x;
        sy = y;
        sd = d;
        break;
      }
    }

    cout << (sy + 1) << " " << (sx + 1) << " ";
    switch (sd) {
    case 0:
      cout << "N";
      break;
    case 1:
      cout << "E";
      break;
    case 2:
      cout << "S";
      break;
    case 3:
      cout << "W";
      break;
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)

int main() {
  while (1) {
    int H, W, L;
    char C[100][100];

    cin >> H >> W >> L;
    if (H == 0)
      return 0;

    int sx, sy, sd;

    rep(y, 0, H) rep(x, 0, W) {
      char c;
      cin >> c;
      if (c != '.' && c != '#') {
        sx = x;
        sy = y;
        switch (c) {
        case 'N':
          sd = 0;
          break;
        case 'E':
          sd = 1;
          break;
        case 'S':
          sd = 2;
          break;
        case 'W':
          sd = 3;
          break;
        }
        C[y][x] = '.';
      } else
        C[y][x] = c;
    }

    int memo[100][100][4];
    rep(y, 0, H) rep(x, 0, W) rep(i, 0, 4) memo[y][x][i] = -1;
    bool noloop = true;

    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {-1, 0, 1, 0};

    rep(l, 0, L) {
      rep(i, 0, 4) {
        int d = (sd + i) % 4;
        if (noloop) {
          if (memo[sy][sx][d] < 0) {
            memo[sy][sx][d] = l;
          } else {
            int dl = l - memo[sy][sx][d];
            l = l + ((L - 1 - l) / dl) * dl;
            noloop = false;
          }
        }
        int x = sx + dx[d];
        int y = sy + dy[d];
        if (x < 0 || W <= x)
          continue;
        if (y < 0 || H <= y)
          continue;
        if (C[y][x] == '#')
          continue;

        sx = x;
        sy = y;
        sd = d;
        break;
      }
    }

    cout << (sy + 1) << " " << (sx + 1) << " ";
    switch (sd) {
    case 0:
      cout << "N";
      break;
    case 1:
      cout << "E";
      break;
    case 2:
      cout << "S";
      break;
    case 3:
      cout << "W";
      break;
    }
    cout << endl;
  }
}