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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 110;
int H, W, cnt;
int sx, sy, sd;
long long L;
char grid[maxn][maxn];
int vis[maxn][maxn][4], id[maxn][maxn][4];
int di[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int DIR(char d) {
  if (d == 'S')
    return 2;
  if (d == 'W')
    return 3;
  if (d == 'N')
    return 0;
  return 1;
}
int move(int &x, int &y, int &d) {
  int tx, ty;
  for (int i = 0; i < 4; i++) {
    tx = x + di[d][0];
    ty = y + di[d][1];
    if (tx < 1 || tx > H || ty < 1 || ty > W || grid[tx][ty] == '#') {
      d = (d + 1) % 4;
      continue;
    }
    break;
  }
  x = tx, y = ty;
  if (vis[x][y][d])
    return id[x][y][d];
  vis[x][y][d] = 1;
  id[x][y][d] = cnt++;
  return 0;
}
int main() {
  // freopen("in.txt","r",stdin);
  while (scanf("%d%d%lld", &H, &W, &L) != EOF, H + W + L) {
    for (int i = 1; i <= H; i++) {
      scanf("%s", grid[i] + 1);
      for (int j = 1; j <= W; j++) {
        if (grid[i][j] == '.' || grid[i][j] == '#')
          continue;
        sx = i, sy = j;
        sd = DIR(grid[i][j]);
      }
    }
    memset(vis, 0, sizeof(vis));
    memset(id, 0, sizeof(id));
    bool flag = true;
    vis[sx][sy][sd] = 1;
    id[sx][sy][sd] = 1;
    cnt = -1;
    while (L) {
      int tv = move(sx, sy, sd);
      if (tv && flag) {
        flag = false;
        L = L % (cnt - tv) + cnt - tv;
      }
      L--;
    }
    char ansd;
    if (sd == 0)
      ansd = 'N';
    else if (sd == 1)
      ansd = 'E';
    else if (sd == 2)
      ansd = 'S';
    else
      ansd = 'W';
    printf("%d %d %c\\n", sx, sy, ansd);
  }
  return 0;
}

Error: 0

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int N = 110;
char s[N][N];
int n, m, cnt, num;
int st, ed;
struct road {
  int x, y;
} vc[N * N * N];

struct eg {
  int dir[4];
  int step[4];
} p[N][N];

int check(char c) {
  if (c == 'N')
    return 0;
  if (c == 'E')
    return 1;
  if (c == 'S')
    return 2;
  if (c == 'W')
    return 3;
  return -1;
}

void solve(int x, int y) {
  int d = check(s[x][y]);
  int xx, yy, flag;
  while (1) {
    xx = x + dx[d];
    yy = y + dy[d];
    flag = 0;
    while (xx < 0 || xx >= n || yy < 0 || yy >= m || s[xx][yy] == '#') {
      d = (d + 1) % 4;
      xx = x + dx[d];
      yy = y + dy[d];
      flag = 1;
    }
    if (p[x][y].dir[d] && flag) {
      st = p[x][y].step[d];
      ed = cnt;
      break;
    }
    x = xx, y = yy;
    p[x][y].dir[d] = 1;
    p[x][y].step[d] = ++cnt;
    vc[num].x = x;
    vc[num].y = y;
    num++;
  }
}

char sth(long long l, int tt) {
  l += tt;
  int x = vc[l].x, y = vc[l].y;
  int xx = vc[l - 1].x, yy = vc[l - 1].y;
  if (xx + 1 == x && yy == y)
    return 'S';
  if (xx - 1 == x && yy == y)
    return 'N';
  if (xx == x && yy + 1 == y)
    return 'E';
  if (xx == x && yy - 1 == y)
    return 'W';
  return 'F';
}

int main() {
  long long l;
  while (cin >> n >> m >> l) {
    if (n == 0 && m == 0 && l == 0)
      break;
    int x, y, flag = 1;
    memset(p, 0, sizeof(p));
    for (int i = 0; i < n; i++) {
      scanf("%s", s[i]);
      for (int j = 0; j < m && flag; j++)
        if (s[i][j] != '.' && s[i][j] != '#') {
          x = i, y = j;
          flag = 0;
        }
    }
    cnt = num = 0;
    // vc.push_back(make_pair(x,y));
    vc[num].x = x, vc[num].y = y;
    num++;
    p[x][y].dir[check(s[x][y])] = 1;
    solve(x, y);
    if (l < ed) {
      printf("%d %d %c\\n", vc[l].x + 1, vc[l].y + 1, sth(l, 0));
    } else {
      l = l - st + 1;
      int r = ed - st;
      l = l % r;
      printf("%d %d %c\\n", vc[st + l - 1].x + 1, vc[st + l - 1].y + 1,
             sth(l, st - 1));
    }
  }
  return 0;
}