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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1e13;
const int N = 1e6 + 4;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};
int n, m, K;
int sx, sy, tx, ty;
char s[N];
struct Node {
  int dir, x, y;
};
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
inline void smin(ll &x, ll y) { x = x > y ? y : x; }
inline ll f(ll x) {
  ll res = x % K;
  return res ? x + K - res : x;
}
int main() {
  //	freopen("in4.txt","r",stdin);
  n = read(), m = read(), K = read();
  sx = read(), sy = read(), tx = read(), ty = read();
  vector<vector<vector<ll>>> dis(
      4, vector<vector<ll>>(n + 1, vector<ll>(m + 1, INF)));
  vector<vector<vector<bool>>> vis(
      4, vector<vector<bool>>(n + 1, vector<bool>(m + 1, false)));
  vector<vector<char>> mp(n + 1, vector<char>(m + 1, '\\0'));
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; ++j)
      mp[i][j] = s[j];
  }
  // SPFA
  queue<Node> q;
  for (int dir = 0; dir < 4; ++dir) {
    dis[dir][sx][sy] = 0;
    vis[dir][sx][sy] = true;
    q.push((Node){dir, sx, sy});
  }
  while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    int cdir = cur.dir, cx = cur.x, cy = cur.y;
    vis[cdir][cx][cy] = false;
    int nx = cx + dx[cdir], ny = cy + dy[cdir];
    if (nx && nx <= n && ny && ny <= m && mp[nx][ny] == '.') {
      // when this is false, mp[][] might explode if nx==n+1 or ny==m+1
      if (dis[cdir][nx][ny] > dis[cdir][cx][cy] + 1) {
        dis[cdir][nx][ny] = dis[cdir][cx][cy] + 1;
        if (!vis[cdir][nx][ny]) {
          vis[cdir][nx][ny] = true;
          q.push((Node){cdir, nx, ny});
        }
      }
    }
    for (int dir = 0; dir < 4; ++dir) {
      if (dis[dir][cx][cy] > f(dis[cdir][cx][cy])) {
        dis[dir][cx][cy] = f(dis[cdir][cx][cy]);
        if (!vis[dir][cx][cy]) {
          vis[dir][cx][cy] = true;
          q.push((Node){dir, cx, cy});
        }
      }
    }
  } /*
   for (int dir=0;dir<4;++dir) {
           for (int i=1;i<=n;++i) {
                   for (int j=1;j<=m;++j)
                           printf("%lld ",dis[dir][i][j]);
                   puts("");
           }
           puts("");
   }*/
  ll ans = INF;
  for (int dir = 0; dir < 4; ++dir) {
    dis[dir][tx][ty] = f(dis[dir][tx][ty]);
    smin(ans, dis[dir][tx][ty]);
  }
  printf("%d\\n", ans < INF ? ans / K : -1);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double INF = 1e9 + 7;
const int N = 1e6 + 4;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};
int n, m, K;
int sx, sy, tx, ty;
char s[N];
struct Node {
  int dir, x, y;
};
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
inline void smin(double &x, double y) { x = x > y ? y : x; }
inline void print() {}
int main() {
  //	freopen("in4.txt","r",stdin);
  n = read(), m = read(), K = read();
  sx = read(), sy = read(), tx = read(), ty = read();
  vector<vector<vector<double>>> dis(
      4, vector<vector<double>>(n + 1, vector<double>(m + 1, INF)));
  vector<vector<vector<bool>>> vis(
      4, vector<vector<bool>>(n + 1, vector<bool>(m + 1, false)));
  vector<vector<char>> mp(n + 1, vector<char>(m + 1));
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; ++j)
      mp[i][j] = s[j];
  }
  // SPFA
  queue<Node> q;
  for (int dir = 0; dir < 4; ++dir) {
    dis[dir][sx][sy] = 0;
    vis[dir][sx][sy] = true;
    q.push((Node){dir, sx, sy});
  }
  double delta = 1.0 / K;
  while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    int cdir = cur.dir, cx = cur.x, cy = cur.y;
    vis[cdir][cx][cy] = false;
    int nx = cx + dx[cdir], ny = cy + dy[cdir];
    if (mp[nx][ny] == '.' && nx && nx <= n && ny && ny <= m) {
      if (dis[cdir][nx][ny] > dis[cdir][cx][cy] + delta) {
        dis[cdir][nx][ny] = dis[cdir][cx][cy] + delta;
        if (!vis[cdir][nx][ny]) {
          vis[cdir][nx][ny] = true;
          q.push((Node){cdir, nx, ny});
        }
      }
    }
    for (int dir = 0; dir < 4; ++dir) {
      if (dis[dir][cx][cy] > ceil(dis[cdir][cx][cy])) {
        dis[dir][cx][cy] = ceil(dis[cdir][cx][cy]);
        if (!vis[dir][cx][cy]) {
          vis[dir][cx][cy] = true;
          q.push((Node){dir, cx, cy});
        }
      }
    }
  }
  double ans = INF;
  for (int dir = 0; dir < 4; ++dir) {
    dis[dir][tx][ty] = ceil(dis[dir][tx][ty]);
    smin(ans, dis[dir][tx][ty]);
  }
  printf("%d\\n", ans < INF ? (int)ans : -1);
  return 0;
}