Problem p01437 (C++) — buggy submission, verdict: Runtime Error

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

struct State {
  int h, w, dir;
};

int dh[] = {-1, 0, 1, 0};
int dw[] = {0, 1, 0, -1};
string dmpi = "NESW";

void out(State st) {
  cout << st.h << " " << st.w << " " << dmpi[st.dir] << endl;
}

int solve(int H, int W, ll L) {
  vector<string> f(H + 2);
  for (int i = 0; i < W + 2; i++) {
    f[0] += "#";
    f[H + 1] += "#";
  }
  State ini = {-1, -1, -1};
  for (int i = 0; i < H; i++) {
    cin >> f[i + 1];
    f[i + 1] = "#" + f[i + 1] + "#";
    assert(f[i + 1].size() == W + 2);
    for (int j = 0; j < W + 2; j++) {
      if (f[i + 1][j] == 'N') {
        ini = State{i + 1, j, 0};
      } else if (f[i + 1][j] == 'E') {
        ini = State{i + 1, j, 1};
      } else if (f[i + 1][j] == 'S') {
        ini = State{i + 1, j, 2};
      } else if (f[i + 1][j] == 'W') {
        ini = State{i + 1, j, 3};
      }
    }
  }
  assert(ini.h != -1);
  vector<vector<vector<int>>> dp(
      H + 2, vector<vector<int>>(W + 2, vector<int>(4, -1)));
  int lps = 0;
  State st = ini;
  int cnt = 0;
  queue<State> que;
  while (true) {
    if (cnt == L) {
      out(st);
      return 0;
    }
    if (dp[st.h][st.w][st.dir] != -1) {
      lps = cnt - dp[st.h][st.w][st.dir];
      break;
    }
    dp[st.h][st.w][st.dir] = cnt;
    if (f[st.h + dh[st.dir]][st.w + dw[st.dir]] == '#') {
      st.dir = (st.dir + 1) % 4;
      continue;
    }
    st.h += dh[st.dir];
    st.w += dw[st.dir];
    que.push(st);
    cnt++;
  }
  L -= cnt + 1;
  vector<State> lpv;
  while (que.size() > lps)
    que.pop();
  while (!que.empty())
    lpv.push_back(que.front()), que.pop();
  assert(lpv.size() == lps);
  L %= lps;
  assert(L < lpv.size());
  out(lpv[L]);
  return 0;
}
int main() {
  int H, W;
  ll L;
  while (cin >> H >> W >> L, H) {
    solve(H, W, L);
  }
  return 0;
}


Error: 0

Fixed submission:

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

struct State {
  int h, w, dir;
};

int dh[] = {-1, 0, 1, 0};
int dw[] = {0, 1, 0, -1};
string dmpi = "NESW";

void out(State st) {
  cout << st.h << " " << st.w << " " << dmpi[st.dir] << endl;
}

int solve(int H, int W, int L) {
  vector<string> f(H + 2);
  for (int i = 0; i < W + 2; i++) {
    f[0] += "#";
    f[H + 1] += "#";
  }
  State ini = {-1, -1, -1};
  for (int i = 0; i < H; i++) {
    cin >> f[i + 1];
    f[i + 1] = "#" + f[i + 1] + "#";
    for (int j = 0; j < W + 2; j++) {
      if (f[i + 1][j] == 'N') {
        ini = State{i + 1, j, 0};
      }
      if (f[i + 1][j] == 'E') {
        ini = State{i + 1, j, 1};
      }
      if (f[i + 1][j] == 'S') {
        ini = State{i + 1, j, 2};
      }
      if (f[i + 1][j] == 'W') {
        ini = State{i + 1, j, 3};
      }
    }
  }
  vector<vector<vector<int>>> dp(
      H + 2, vector<vector<int>>(W + 2, vector<int>(4, -1)));
  int lps = 0;
  State st = ini;
  int cnt = 0;
  queue<State> que;
  while (true) {
    if (cnt == L) {
      out(st);
      return 0;
    }
    if (dp[st.h][st.w][st.dir] != -1) {
      lps = cnt - dp[st.h][st.w][st.dir];
      break;
    }
    dp[st.h][st.w][st.dir] = cnt;
    if (f[st.h + dh[st.dir]][st.w + dw[st.dir]] == '#') {
      st.dir = (st.dir + 1) % 4;
      continue;
    }
    st.h += dh[st.dir];
    st.w += dw[st.dir];
    que.push(st);
    cnt++;
  }
  L -= cnt + 1;
  vector<State> lpv;
  while (que.size() > lps)
    que.pop();
  while (!que.empty())
    lpv.push_back(que.front()), que.pop();
  L %= lps;
  out(lpv[L]);
  return 0;
}
int main() {
  int H, W, L;
  while (cin >> H >> W >> L, H) {
    solve(H, W, L);
  }
  return 0;
}