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

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
using T = tuple<int, int, int>;

int main() {
  int H, W;
  int64_t L;
  while (cin >> H >> W >> L, H) {
    char b[H + 2][W + 2];
    fill(b[0], b[H + 2], '#');
    int y, x, d;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> b[i][j];
        if (b[i][j] != '.' && b[i][j] != '#') {
          y = i;
          x = j;
          switch (b[i][j]) {
          case 'N':
            d = 0;
            break;
          case 'E':
            d = 1;
            break;
          case 'S':
            d = 2;
            break;
          case 'W':
            d = 3;
            break;
          }
        }
      }
    }
    int64_t v[H + 2][W + 2][4];
    fill(v[0][0], v[H + 2][0], 0);
    vector<T> V;
    int dy[4] = {-1, 0, 1, 0};
    int dx[4] = {0, 1, 0, -1};
    string s = "NESW";
    bool flag = false;
    while (true) {
      // cout<<' '<<y<<' '<<x<<' '<<d<<endl;
      if (b[y + dy[d]][x + dx[d]] != '#') {
        if (v[y + dy[d]][x + dx[d]][d] && !flag) {
          flag = true;
          L = v[y + dy[d]][x + dx[d]][d] +
              (L - v[y + dy[d]][x + dx[d]][d]) %
                  (v[y][x][d] - v[y + dy[d]][x + dx[d]][d] + 1);
        } else {
          v[y + dy[d]][x + dx[d]][d] = v[y][x][d] + 1;
        }
        y += dy[d];
        x += dx[d];
        if (v[y][x][d] == L) {
          cout << y << ' ' << x << ' ' << s[d] << endl;
          break;
        }
      } else {
        v[y][x][(d + 1) % 4] = v[y][x][d];
        d = (d + 1) % 4;
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
using T = tuple<int, int, int>;

int main() {
  int H, W, L;
  while (cin >> H >> W >> L, H) {
    char b[H + 2][W + 2];
    fill(b[0], b[H + 2], '#');
    int y, x, d;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> b[i][j];
        if (b[i][j] != '.' && b[i][j] != '#') {
          y = i;
          x = j;
          switch (b[i][j]) {
          case 'N':
            d = 0;
            break;
          case 'E':
            d = 1;
            break;
          case 'S':
            d = 2;
            break;
          case 'W':
            d = 3;
            break;
          }
        }
      }
    }
    int v[H + 2][W + 2];
    fill(v[0], v[H + 2], 0);
    vector<T> V;
    int dy[4] = {-1, 0, 1, 0};
    int dx[4] = {0, 1, 0, -1};
    string s = "NESW";
    bool flag = false;
    while (true) {
      // cout<<' '<<y<<' '<<x<<' '<<d<<endl;
      if (b[y + dy[d]][x + dx[d]] != '#') {
        if (v[y + dy[d]][x + dx[d]] && !flag) {
          flag = true;
          L = v[y + dy[d]][x + dx[d]] +
              (L - v[y + dy[d]][x + dx[d]]) %
                  (v[y][x] - v[y + dy[d]][x + dx[d]] + 1);
        } else {
          v[y + dy[d]][x + dx[d]] = v[y][x] + 1;
        }
        y += dy[d];
        x += dx[d];
        if (v[y][x] == L) {
          cout << y << ' ' << x << ' ' << s[d] << endl;
          break;
        }
      } else {
        d = (d + 1) % 4;
      }
    }
  }

  return 0;
}