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

#include <bits/stdc++.h>

using namespace std;

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

char cells[100][100];
int visited[100][100][4];
int visitt[100][100][4];
int visitbef[100][100][2];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
char d[4] = {'N', 'E', 'S', 'W'};

int main() {
  while (1) {
    int posx, posy, befx, befy, dir;
    int h, w, walk = 0, flag = 0;
    long long l;
    cin >> h >> w >> l;
    if (h == 0)
      break;
    rep(i, h) {
      string s;
      cin >> s;
      rep(j, w) {
        cells[i][j] = s[j];
        if (s[j] != '.' && s[j] != '#') {
          posx = i;
          posy = j;
          if (s[j] == 'N') {
            dir = 0;
          } else if (s[j] == 'E') {
            dir = 1;
          } else if (s[j] == 'S') {
            dir = 2;
          } else {
            dir = 3;
          }
        }
      }
    }
    rep(i, h) rep(j, w) rep(k, 4) {
      visited[i][j][k] = 0;
      visitt[i][j][k] = -1;
    }
    visited[posx][posy][dir] = 1;
    visitt[posx][posy][dir] = 0;
    while (l > 0) {
      if (posx + dx[dir] >= 0 && posx + dx[dir] < h && posy + dy[dir] >= 0 &&
          posy + dy[dir] < w) {
        if (cells[posx + dx[dir]][posy + dy[dir]] != '#') {
          befx = posx;
          befy = posy;
          posx += dx[dir];
          posy += dy[dir];
          cells[posx][posy] = '@';
          if (visited[posx][posy][dir] == 0) {
            l--;
            walk++;
            visited[posx][posy][dir] = 1;
            visitt[posx][posy][dir] = walk;
            visitbef[posx][posy][0] = befx;
            visitbef[posx][posy][1] = befy;
          } else if (flag == 0 && visitbef[posx][posy][0] == befx &&
                     visitbef[posx][posy][1] == befy) {
            l = l % (walk - visitt[posx][posy][dir] + 1) - 1 +
                (walk - visitt[posx][posy][dir] + 1);
            flag = 1;
          } else if (flag == 0) {
            l--;
            walk++;
            visitt[posx][posy][dir] = walk;
          } else {
            l--;
          }
        } else {
          dir++;
          if (dir == 4)
            dir = 0;
          if (visited[posx][posy][dir] == 0) {
            visited[posx][posy][dir] = 1;
            visitt[posx][posy][dir] = walk;
          } else if (flag == 0 && visitbef[posx][posy][0] == befx &&
                     visitbef[posx][posy][1] == befy &&
                     cells[posx + dx[dir - 1]][posy + dy[dir - 1]] != '#' &&
                     posx + dx[dir - 1] >= 0 && posx + dx[dir - 1] < h &&
                     posy + dy[dir - 1] >= 0 && posy + dy[dir - 1] < w) {
            l = l % (walk - visitt[posx][posy][dir]);
            flag = 1;
          }
        }
      } else {
        dir++;
        if (dir == 4)
          dir = 0;
        if (visited[posx][posy][dir] == 0) {
          visited[posx][posy][dir] = 1;
          visitt[posx][posy][dir] = walk;
        } else if (flag == 0 && visitbef[posx][posy][0] == befx &&
                   visitbef[posx][posy][1] == befy &&
                   cells[posx + dx[dir - 1]][posy + dy[dir - 1]] != '#' &&
                   posx + dx[dir - 1] >= 0 && posx + dx[dir - 1] < h &&
                   posy + dy[dir - 1] >= 0 && posy + dy[dir - 1] < w) {
          l = l % (walk - visitt[posx][posy][dir]) +
              (walk - visitt[posx][posy][dir]);
          flag = 1;
        }
      }
    }
    cout << posx + 1 << " " << posy + 1 << " " << d[dir] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

char cells[100][100];
int visited[100][100][4];
int visitt[100][100][4];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
char d[4] = {'N', 'E', 'S', 'W'};

int main() {
  while (1) {
    int posx, posy, dir;
    int h, w, l, walk = 0, flag = 0;
    cin >> h >> w >> l;
    if (h == 0)
      break;
    rep(i, h) {
      string s;
      cin >> s;
      rep(j, w) {
        cells[i][j] = s[j];
        if (s[j] != '.' && s[j] != '#') {
          posx = i;
          posy = j;
          if (s[j] == 'N') {
            dir = 0;
          } else if (s[j] == 'E') {
            dir = 1;
          } else if (s[j] == 'S') {
            dir = 2;
          } else {
            dir = 3;
          }
        }
      }
    }
    rep(i, h) rep(j, w) rep(k, 4) {
      visited[i][j][k] = 0;
      visitt[i][j][k] = -1;
    }
    visited[posx][posy][dir] = 1;
    visitt[posx][posy][dir] = 0;
    while (l > 0) {
      if (posx + dx[dir] >= 0 && posx + dx[dir] < h && posy + dy[dir] >= 0 &&
          posy + dy[dir] < w) {
        if (cells[posx + dx[dir]][posy + dy[dir]] != '#') {
          posx += dx[dir];
          posy += dy[dir];
          if (visited[posx][posy][dir] == 0) {
            l--;
            walk++;
            visited[posx][posy][dir] = 1;
            visitt[posx][posy][dir] = walk;
          } else if (flag == 0) {
            l = l % (walk - visitt[posx][posy][dir] + 1) - 1;
            flag = 1;
          } else {
            l--;
          }
        } else {
          dir++;
          if (dir == 4)
            dir = 0;
          if (visited[posx][posy][dir] == 0) {
            visited[posx][posy][dir] = 1;
            visitt[posx][posy][dir] = walk;
          } else if (flag == 0) {
            l = l % (walk - visitt[posx][posy][dir]);
            flag = 1;
          }
        }
      } else {
        dir++;
        if (dir == 4)
          dir = 0;
        if (visited[posx][posy][dir] == 0) {
          visited[posx][posy][dir] = 1;
          visitt[posx][posy][dir] = walk;
        } else if (flag == 0) {
          l = l % (walk - visitt[posx][posy][dir]);
          flag = 1;
        }
      }
    }
    cout << posx + 1 << " " << posy + 1 << " " << d[dir] << endl;
  }
  return 0;
}