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

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
typedef long long int ll;

struct State {
  int x, y, dir;
};

int N, M;
ll L;
char board[110][110];
State nxt[110][110][4][70];
string pat = "NESW";

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

int main() {
  while (cin >> N >> M >> L, N || M || L) {
    int sx, sy, d;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        cin >> board[i][j];
        if (pat.find(board[i][j]) != string::npos) {
          sx = i, sy = j, d = pat.find(board[i][j]);
        }
      }
    }

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (board[i][j] == '#')
          continue;
        for (int k = 0; k < 4; k++) {
          int d = k;
          int cnt_ng = 0;
          while (cnt_ng < 4) {
            int nx = i + dx[d], ny = j + dy[d];

            bool ok = true;
            if (nx < 0 || nx >= N || ny < 0 || ny >= M)
              ok = false;
            if (board[nx][ny] == '#')
              ok = false;
            if (ok) {
              nxt[i][j][k][0] = State{nx, ny, d};
              break;
            } else {
              cnt_ng++;
              d = (d + 1) % 4;
            }
          }
        }
      }
    }

    for (int m = 1; m < 65; m++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
          for (int k = 0; k < 4; k++) {
            State t = nxt[i][j][k][m - 1];
            nxt[i][j][k][m] = nxt[t.x][t.y][t.dir][m - 1];
          }
        }
      }
    }

    State ans{sx, sy, d};
    for (int b = 0; b < 60; b++) {
      if (L >> b & 1) {
        ans = nxt[ans.x][ans.y][ans.dir][b];
      }
    }
    printf("%d %d %c\\n", ans.x + 1, ans.y + 1, pat[ans.dir]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
typedef long long int ll;

struct State {
  int x, y, dir;
};

int N, M;
ll L;
char board[110][110];
State nxt[110][110][4][70];
string pat = "NESW";

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

int main() {
  while (cin >> N >> M >> L, N || M || L) {
    int sx, sy, d;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        cin >> board[i][j];
        int f = pat.find(board[i][j]);
        if (f != (int)string::npos) {
          sx = i, sy = j, d = f;
        }
      }
    }

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (board[i][j] == '#')
          continue;
        for (int k = 0; k < 4; k++) {
          int d = k;
          while (1) {
            int nx = i + dx[d], ny = j + dy[d];

            bool ok = true;
            if (nx < 0 || nx >= N || ny < 0 || ny >= M)
              ok = false;
            if (board[nx][ny] == '#')
              ok = false;
            if (ok) {
              nxt[i][j][k][0] = State{nx, ny, d};
              break;
            } else {
              d = (d + 1) % 4;
            }
          }
        }
      }
    }

    for (int m = 1; m < 65; m++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          for (int k = 0; k < 4; k++) {
            State t = nxt[i][j][k][m - 1];
            nxt[i][j][k][m] = nxt[t.x][t.y][t.dir][m - 1];
          }
        }
      }
    }

    State ans{sx, sy, d};
    for (int b = 0; b < 60; b++) {
      if (L >> b & 1) {
        ans = nxt[ans.x][ans.y][ans.dir][b];
      }
    }
    printf("%d %d %c\\n", ans.x + 1, ans.y + 1, pat[ans.dir]);
  }
  return 0;
}