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

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
#define int long long
using namespace std;
string s[110];
vector<pair<pair<int, int>, char>> v;
int h, w, l, x, y, syuki, pp;
char t;
int used[101][101][25];
void dig() {
  int sum = 1;
  while (1) {
    int p = 0;
    if (used[y][x][(int)t - 'C']) {
      syuki = sum - used[y][x][t - 'C'];
      pp = sum - syuki;
      break;
    }
    used[y][x][t - 'C'] = sum;
    if (t == 'N') {
      if (s[y - 1][x] == '#')
        t = 'E', p++;
      else
        y--;
    } else if (t == 'E') {
      if (s[y][x + 1] == '#')
        t = 'S', p++;
      else
        x++;
    } else if (t == 'S') {
      if (s[y + 1][x] == '#')
        t = 'W', p++;
      else
        y++;
    } else {
      if (s[y][x - 1] == '#')
        t = 'N', p++;
      else
        x--;
    }
    if (!p)
      v.push_back(mk(mk(y, x), t)), sum++;
  }
}
main() {
  while (cin >> h >> w >> l, w) {
    r(i, h + 2) s[i] = "";
    r(i, h) cin >> s[i + 1];
    r(i, w + 2) s[0] += '#', s[h + 1] += '#';
    r(i, h) s[i + 1] = '#' + s[i + 1] + '#';
    v.clear();
    memset(used, 0, sizeof(used));
    r(i, h + 2) r(j, w + 2) if (isalpha(s[i][j])) x = j, y = i, t = s[i][j];
    dig();
    int a = syuki;
    if (l < v.size())
      cout << v[l - 1].first.first << ' ' << v[l - 1].first.second << ' '
           << v[l - 1].second << endl;
    else {
      while ((int)v.size() > syuki)
        v.erase(v.begin());

      cout << v[(l - pp) % a].first.first << ' ' << v[(l - pp) % a].first.second
           << ' ' << v[(l - pp) % a].second << endl;
    }
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
using namespace std;
string s[110];
vector<pair<pair<int, int>, char>> v;
int h, w, l, x, y, syuki, pp;
char t;
int used[101][101][25];
void dig() {
  int sum = 1;
  while (1) {
    int p = 0;
    if (used[y][x][(int)t - 'C']) {
      syuki = sum - used[y][x][t - 'C'];
      pp = sum - syuki;
      break;
    }
    used[y][x][t - 'C'] = sum;
    if (t == 'N') {
      if (s[y - 1][x] == '#')
        t = 'E', p++;
      else
        y--;
    } else if (t == 'E') {
      if (s[y][x + 1] == '#')
        t = 'S', p++;
      else
        x++;
    } else if (t == 'S') {
      if (s[y + 1][x] == '#')
        t = 'W', p++;
      else
        y++;
    } else {
      if (s[y][x - 1] == '#')
        t = 'N', p++;
      else
        x--;
    }
    if (!p)
      v.push_back(mk(mk(y, x), t)), sum++;
  }
}
int main() {
  while (cin >> h >> w >> l, w) {
    r(i, h + 2) s[i] = "";
    r(i, h) cin >> s[i + 1];
    r(i, w + 2) s[0] += '#', s[h + 1] += '#';
    r(i, h) s[i + 1] = '#' + s[i + 1] + '#';
    v.clear();
    memset(used, 0, sizeof(used));
    r(i, h + 2) r(j, w + 2) if (isalpha(s[i][j])) x = j, y = i, t = s[i][j];
    dig();
    int a = syuki;
    if (l < v.size())
      cout << v[l].first.first << ' ' << v[l].first.second << ' ' << v[l].second
           << endl;
    else {
      while ((int)v.size() > syuki)
        v.erase(v.begin());

      cout << v[(l - pp) % a].first.first << ' ' << v[(l - pp) % a].first.second
           << ' ' << v[(l - pp) % a].second << endl;
    }
  }
}