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

#include <iostream>
using namespace std;
long long H, W, L;
char x[102][102], T[5] = "NESW";
int dp[102][102][4];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  while (true) {
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        x[i][j] = '#';
        for (int k = 0; k < 4; k++)
          dp[i][j][k] = -1;
      }
    }
    cin >> H >> W >> L;
    int D = 0, X = 0, Y = 0;
    if (H == 0 && W == 0 && L == 0)
      break;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> x[i][j];
        if (x[i][j] != '#' && x[i][j] != '.') {
          for (int k = 0; k < 4; k++) {
            if (T[k] == x[i][j]) {
              x[i][j] = '.';
              D = k;
              X = i;
              Y = j;
            }
          }
        }
      }
    }
    int cnt = 0;
    dp[X][Y][D] = 0;
    long long V = 0, Z = 0, C = 0, F = 0;
    while (true) {
      if (x[X + dx[D]][Y + dy[D]] == '#') {
        D = (D + 1) % 4;
      } else {
        X += dx[D];
        Y += dy[D];
        cnt++;
        if (dp[X][Y][D] != -1) {
          V = dp[X][Y][D];
          Z = cnt;
          goto E;
        } else {
          dp[X][Y][D] = cnt;
          if (cnt == L) {
            cout << X << ' ' << Y << ' ' << T[D] << endl;
            goto F;
          }
        }
      }
    }
  E:;
    C = (Z - V);
    F = (L - Z) % C;
    F += V;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        for (int k = 0; k < 4; k++) {
          if (dp[i][j][k] == F) {
            cout << i << ' ' << j << ' ' << T[k] << endl;
          }
        }
      }
    }
  F:;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
long long H, W, L;
char x[102][102], T[5] = "NESW";
int dp[102][102][4];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  while (true) {
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        x[i][j] = '#';
        for (int k = 0; k < 4; k++)
          dp[i][j][k] = -1;
      }
    }
    cin >> H >> W >> L;
    int D = 0, X = 0, Y = 0;
    if (H == 0 && W == 0 && L == 0)
      break;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> x[i][j];
        if (x[i][j] != '#' && x[i][j] != '.') {
          for (int k = 0; k < 4; k++) {
            if (T[k] == x[i][j]) {
              x[i][j] = '.';
              D = k;
              X = i;
              Y = j;
            }
          }
        }
      }
    }
    int cnt = 0;
    dp[X][Y][D] = 0;
    long long V = 0, W = 0, C = 0, F = 0;
    while (true) {
      if (x[X + dx[D]][Y + dy[D]] == '#') {
        D = (D + 1) % 4;
      } else {
        X += dx[D];
        Y += dy[D];
        cnt++;
        if (dp[X][Y][D] != -1) {
          V = dp[X][Y][D];
          W = cnt;
          goto E;
        } else {
          dp[X][Y][D] = cnt;
          if (cnt == L) {
            cout << X << ' ' << Y << ' ' << T[D] << endl;
            goto F;
          }
        }
      }
    }
  E:;
    C = (W - V);
    F = (L - W) % C;
    F += V;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        for (int k = 0; k < 4; k++) {
          if (dp[i][j][k] == F) {
            cout << i << ' ' << j << ' ' << T[k] << endl;
          }
        }
      }
    }
  F:;
  }
  return 0;
}