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

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define int long long
const int MOD = 1e9 + 7;
const int N = 1e3 + 10;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int32_t main() {
  IOS;
  int n, m, K;
  cin >> n >> m >> K;
  int x, y, source, destination;
  cin >> source >> destination >> x >> y;
  char xxx;
  vector<vector<int>> grid(n + 2, vector<int>(m + 2, 0));

  int dis[n + 4][m + 4][6];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      grid[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {

      cin >> xxx;
      if (xxx == '.') {
        grid[i][j] = 1;
      }
      for (int k = 0; k < 4; k++) {
        dis[i][j][k] = MOD;
      }
    }
  }

  queue<pair<int, pair<int, int>>> q;
  for (int i = 0; i < 4; i++) {
    dis[x][y][i] = 0;
    q.push(make_pair(x, make_pair(y, i)));
  }
  pair<int, pair<int, int>> v;
  int xx, yy;
  int d;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    // cout<<v.ff<<" "<<v.ss.ff<<" "<<v.ss.ss<<endl;
    for (int k = 0; k < 4; k++) {
      xx = v.ff + dx[k];
      yy = v.ss.ff + dy[k];
      if (!grid[xx][yy])
        continue;
      d = dis[v.ff][v.ss.ff][v.ss.ss];
      if (k == v.ss.ss) {
        d++;
      } else {
        int kk = d / K + (d % K != 0);
        d = kk * K;
        d++;
      }
      if (dis[xx][yy][k] > d) {

        dis[xx][yy][k] = d;
        q.push(make_pair(xx, make_pair(yy, k)));
      }
    }
  }
  int ans = MOD;
  for (int k = 0; k < 4; k++) {
    ans = min(ans, dis[source][destination][k]);
  }
  if (ans == MOD) {
    cout << "-1" << endl;
    return 0;
  }

  ans = ans / K + (ans % K != 0);
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define int long long
const int MOD = 1e9 + 7;
const int N = 1e3 + 10;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int32_t main() {
  IOS;
  int n, m, K;
  cin >> n >> m >> K;
  int x, y, source, destination;
  cin >> source >> destination >> x >> y;
  char xxx;
  int grid[n + 2][m + 4];
  int dis[n + 4][m + 4][6];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      grid[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {

      cin >> xxx;
      if (xxx == '.') {
        grid[i][j] = 1;
      }
      for (int k = 0; k < 4; k++) {
        dis[i][j][k] = MOD;
      }
    }
  }

  queue<pair<int, pair<int, int>>> q;
  for (int i = 0; i < 4; i++) {
    dis[x][y][i] = 0;
    q.push(make_pair(x, make_pair(y, i)));
  }
  pair<int, pair<int, int>> v;
  int xx, yy;
  int d;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    // cout<<v.ff<<" "<<v.ss.ff<<" "<<v.ss.ss<<endl;
    for (int k = 0; k < 4; k++) {
      xx = v.ff + dx[k];
      yy = v.ss.ff + dy[k];
      if (!grid[xx][yy])
        continue;
      d = dis[v.ff][v.ss.ff][v.ss.ss];
      if (k == v.ss.ss) {
        d++;
      } else {
        int kk = d / K + (d % K != 0);
        d = kk * K;
        d++;
      }
      if (dis[xx][yy][k] > d) {

        dis[xx][yy][k] = d;
        q.push(make_pair(xx, make_pair(yy, k)));
      }
    }
  }
  int ans = MOD;
  for (int k = 0; k < 4; k++) {
    ans = min(ans, dis[source][destination][k]);
  }
  if (ans == MOD) {
    cout << "-1" << endl;
    return 0;
  }

  ans = ans / K + (ans % K != 0);
  cout << ans << endl;
}