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

#include <cstdio>
#include <vector>
std::vector<int> g[1000005];
std::vector<int> vis[1000005];
std::vector<int> dis[1000005];
char s[1000005];
struct Element {
  int x, y;
  Element(int x = 0, int y = 0) : x(x), y(y) {}
};
Element queue[1000005];

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int x1, y1, x2, y2;
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    g[i].push_back(-1);
    vis[i].push_back(0);
    dis[i].push_back(0);
    for (int j = 1; j <= m; j++) {
      if (s[j] == '.')
        g[i].push_back(0);
      else
        g[i].push_back(1);
      vis[i].push_back(0);
      dis[i].push_back(0);
    }
  }
  int h = 0, t = -1;
  queue[++t] = Element(x1, y1);
  vis[x1][y1] = 1;
  dis[x1][y1] = 0;
  while (h <= t) {
    Element u = queue[h++];
    int x = u.x, y = u.y;
    for (int i = 1; i <= k && x + i <= n; i++) {
      if ((vis[x + i][y] && dis[x + i][y] <= dis[x][y]) || g[x + i][y]) {
        break;
      } else if (vis[x + i][y] == 0) {
        vis[x + i][y] = 1;
        dis[x + i][y] = dis[x][y] + 1;
        queue[++t] = Element(x + i, y);
      }
    }
    for (int i = 1; i <= k && x - i >= 1; i++) {
      if ((vis[x - i][y] && dis[x - i][y] <= dis[x][y]) || g[x - i][y])
        break;
      else if (vis[x - i][y] == 0) {
        vis[x - i][y] = 1;
        dis[x - i][y] = dis[x][y] + 1;
        queue[++t] = Element(x - i, y);
      }
    }
    for (int i = 1; i <= k && y + i <= m; i++) {
      if ((vis[x][y + i] && dis[x][y + i] <= dis[x][y]) || g[x][y + i])
        break;
      else if (vis[x][y + i] == 0) {
        vis[x][y + i] = 1;
        dis[x][y + i] = dis[x][y] + 1;
        queue[++t] = Element(x, y + i);
      }
    }
    for (int i = 1; i <= k && y - i >= 1; i++) {
      if ((vis[x][y - i] && dis[x][y - i] <= dis[x][y]) || g[x][y - i])
        break;
      else if (vis[x][y - i] == 0) {
        vis[x][y - i] = 1;
        dis[x][y - i] = dis[x][y] + 1;
        queue[++t] = Element(x, y - i);
      }
    }
  }
  if (!vis[x2][y2])
    printf("-1\\n");
  else
    printf("%d\\n", dis[x2][y2]);
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <vector>
std::vector<int> g[1000005];
std::vector<int> vis[1000005];
std::vector<int> dis[1000005];
char s[1000005];
struct Element {
  int x, y;
  Element(int x = 0, int y = 0) : x(x), y(y) {}
};
Element queue[1000005];

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int x1, y1, x2, y2;
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    g[i].push_back(-1);
    vis[i].push_back(0);
    dis[i].push_back(0);
    for (int j = 1; j <= m; j++) {
      if (s[j] == '.')
        g[i].push_back(0);
      else
        g[i].push_back(1);
      vis[i].push_back(0);
      dis[i].push_back(0);
    }
  }
  int h = 0, t = -1;
  queue[++t] = Element(x1, y1);
  vis[x1][y1] = 1;
  dis[x1][y1] = 0;
  while (h <= t) {
    Element u = queue[h++];
    int x = u.x, y = u.y;
    for (int i = 1; i <= k && x + i <= n; i++) {
      if ((vis[x + i][y] && dis[x + i][y] <= dis[x][y]) || g[x + i][y]) {
        break;
      } else {
        vis[x + i][y] = 1;
        dis[x + i][y] = dis[x][y] + 1;
        queue[++t] = Element(x + i, y);
      }
    }
    for (int i = 1; i <= k && x - i >= 1; i++) {
      if ((vis[x - i][y] && dis[x - i][y] <= dis[x][y]) || g[x - i][y])
        break;
      else {
        vis[x - i][y] = 1;
        dis[x - i][y] = dis[x][y] + 1;
        queue[++t] = Element(x - i, y);
      }
    }
    for (int i = 1; i <= k && y + i <= m; i++) {
      if ((vis[x][y + i] && dis[x][y + i] <= dis[x][y]) || g[x][y + i])
        break;
      else {
        vis[x][y + i] = 1;
        dis[x][y + i] = dis[x][y] + 1;
        queue[++t] = Element(x, y + i);
      }
    }
    for (int i = 1; i <= k && y - i >= 1; i++) {
      if ((vis[x][y - i] && dis[x][y - i] <= dis[x][y]) || g[x][y - i])
        break;
      else {
        vis[x][y - i] = 1;
        dis[x][y - i] = dis[x][y] + 1;
        queue[++t] = Element(x, y - i);
      }
    }
  }
  if (!vis[x2][y2])
    printf("-1\\n");
  else
    printf("%d\\n", dis[x2][y2]);
  return 0;
}