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

#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

void output(int i, int j, int d) {
  cout << i + 1 << ' ' << j + 1 << ' ' << "NESW"[d] << endl;
}

int main() {
  int h, w;
  long long l;
  for (; cin >> h >> w >> l, h | w | l;) {
    vector<string> grid(h);
    for (int i = 0; i < h; i++)
      cin >> grid[i];

    vector<vector<vector<int>>> vis(h,
                                    vector<vector<int>>(w, vector<int>(4, -1)));

    queue<tuple<int, int, int, int>> q; // (i,j,dir,time)
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        int d = string("NESW").find(grid[i][j]);
        if (d != -1) {
          q.emplace(i, j, d, 0);
          grid[i][j] = '.';
        }
      }

    while (q.size()) {
      auto c = q.front();
      q.pop();
      int ci = get<0>(c), cj = get<1>(c), cd = get<2>(c), ct = get<3>(c);
      if (ct == l) {
        output(ci, cj, cd);
        break;
      }
      if (vis[ci][cj][cd] != -1) {
        int len = ct - vis[ci][cj][cd];
        for (int i = 0; i < h; i++)
          for (int j = 0; j < w; j++)
            for (int k = 0; k < 4; k++)
              if (vis[i][j][k] == (l - vis[ci][cj][cd]) % len + vis[ci][cj][cd])
                output(i, j, k);
        break;
      }
      vis[ci][cj][cd] = ct;
      for (int i = 0; i < 4; i++) {
        int nd = (cd + i) % 4, ni = ci + "\\xff\\0\\x1\\0"[nd],
            nj = cj + "\\0\\x1\\0\\xff"[nd];
        if (0 <= ni && ni < h && 0 <= nj && nj < w && grid[ni][nj] != '#') {
          q.emplace(ni, nj, nd, ct + 1);
          break;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

void output(int i, int j, int d) {
  cout << i + 1 << ' ' << j + 1 << ' ' << "NESW"[d] << endl;
}

int main() {
  for (int h, w, l; cin >> h >> w >> l, h | w | l;) {
    vector<string> grid(h);
    for (int i = 0; i < h; i++)
      cin >> grid[i];

    vector<vector<int>> vis(h, vector<int>(w, -1)), dir(h, vector<int>(w, -1));

    queue<tuple<int, int, int, int>> q; // (i,j,dir,time)
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        int d = string("NESW").find(grid[i][j]);
        if (d != -1) {
          q.emplace(i, j, d, 0);
          grid[i][j] = '.';
        }
      }

    while (q.size()) {
      auto c = q.front();
      q.pop();
      int ci = get<0>(c), cj = get<1>(c), cd = get<2>(c), ct = get<3>(c);
      if (ct == l) {
        output(ci, cj, cd);
        break;
      }
      if (vis[ci][cj] != -1) {
        int len = ct - vis[ci][cj];
        for (int i = 0; i < h; i++)
          for (int j = 0; j < w; j++)
            if (vis[i][j] == (l - vis[ci][cj]) % len + vis[ci][cj])
              output(i, j, dir[i][j]);
        break;
      }
      vis[ci][cj] = ct, dir[ci][cj] = cd;
      for (int i = 0; i < 4; i++) {
        int nd = (cd + i) % 4, ni = ci + "\\xff\\0\\x1\\0"[nd],
            nj = cj + "\\0\\x1\\0\\xff"[nd];
        if (0 <= ni && ni < h && 0 <= nj && nj < w && grid[ni][nj] != '#') {
          q.emplace(ni, nj, nd, ct + 1);
          break;
        }
      }
    }
  }
}