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

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define Max(a, b) (a > b ? a : b)
#define Min(a, b) (a < b ? a : b)
#define ii pair<int, int>
#define iii pair<int, ii>
#define fi first
#define se second
const int MAX = 300000;     // 2 * 10^5
const int MOD = 1000000007; // 10^9 + 7
const int OO = 0x3f3f3f3f;  // 0x3f3f3f3f;

int h, w, k;
ii s, e;
vector<vector<char>> M;
vector<vector<vector<int>>> dist;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, -1, 1, 0};

bool check(int x, int y) { return x >= 0 and x < h and y >= 0 and y < w; }

int32_t main() {
  scanf(" %d %d %d", &h, &w, &k);
  scanf(" %d %d %d %d", &s.fi, &s.se, &e.fi, &e.se);

  s.fi--;
  s.se--;
  e.fi--;
  e.se--;

  M.resize(h);
  dist.resize(h);

  for (vector<vector<int>> &v : dist) {
    v.resize(w);
    for (int i = 0; i < v.size(); i++)
      v[i].resize(4, OO);
  }
  for (vector<char> &v : M) {
    v.resize(w);
    for (int i = 0; i < v.size(); i++)
      scanf(" %c", &v[i]);
  }

  priority_queue<array<int, 5>, vector<array<int, 5>>, greater<array<int, 5>>>
      q;
  for (int i = 0; i < 4; i++) {
    dist[s.fi][s.se][i] = 1;
    q.push({1, k, i, s.fi, s.se});
  }
  while (!q.empty()) {
    ii cur = {q.top()[3], q.top()[4]};
    int c = q.top()[1];
    int t = q.top()[2];
    int d = q.top()[0];
    q.pop();

    if (dist[cur.fi][cur.se][t] < d)
      continue;

    for (int i = 0; i < 4; i++) {
      int x = cur.fi + dx[i], y = cur.se + dy[i];
      if (!check(x, y) or M[x][y] != '.')
        continue;
      bool fl = false;
      if (t == i) {
        if (c >= 1) {
          if (dist[x][y][i] > dist[cur.fi][cur.se][t]) {
            dist[x][y][i] = dist[cur.fi][cur.se][t];
            q.push({dist[x][y][i], c - 1, i, x, y});
          }
        } else
          fl = true;
      }
      if (t != i or fl) {
        if (dist[x][y][i] > dist[cur.fi][cur.se][t] + 1) {
          dist[x][y][i] = dist[cur.fi][cur.se][t] + 1;
          q.push({dist[x][y][i], k - 1, i, x, y});
        }
      }
    }
  }
  int ans = OO;
  for (int i = 0; i < 4; i++)
    ans = min(ans, dist[e.fi][e.se][i]);
  if (ans == OO)
    ans = -1;
  cout << ans << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define Max(a, b) (a > b ? a : b)
#define Min(a, b) (a < b ? a : b)
#define ii pair<int, int>
#define iii pair<int, ii>
#define fi first
#define se second
const int MAX = 300000;     // 2 * 10^5
const int MOD = 1000000007; // 10^9 + 7
const int OO = 0x3f3f3f3f;  // 0x3f3f3f3f;

int h, w, k;
ii s, e;
vector<vector<char>> M;
// vector<vector<int>> dist;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, -1, 1, 0};

int dist[100][100][4];

bool check(int x, int y) { return x >= 0 and x < h and y >= 0 and y < w; }

int32_t main() {
  scanf(" %d %d %d", &h, &w, &k);
  scanf(" %d %d %d %d", &s.fi, &s.se, &e.fi, &e.se);

  s.fi--;
  s.se--;
  e.fi--;
  e.se--;

  M.resize(h);
  // dist.resize(h);

  memset(dist, 63, sizeof(dist));
  // for(vector<int> &v : dist)
  //	v.resize(w, OO);

  for (vector<char> &v : M) {
    v.resize(w);
    for (int i = 0; i < v.size(); i++)
      scanf(" %c", &v[i]);
  }

  priority_queue<array<int, 5>, vector<array<int, 5>>, greater<array<int, 5>>>
      q;
  for (int i = 0; i < 4; i++) {
    dist[s.fi][s.se][i] = 1;
    q.push({1, k, i, s.fi, s.se});
  }
  while (!q.empty()) {
    ii cur = {q.top()[3], q.top()[4]};
    int c = q.top()[1];
    int t = q.top()[2];
    int d = q.top()[0];
    q.pop();

    if (dist[cur.fi][cur.se][t] < d)
      continue;

    for (int i = 0; i < 4; i++) {
      int x = cur.fi + dx[i], y = cur.se + dy[i];
      if (!check(x, y) or M[x][y] != '.')
        continue;
      bool fl = false;
      if (t == i) {
        if (c >= 1) {
          if (dist[x][y][i] > dist[cur.fi][cur.se][t]) {
            dist[x][y][i] = dist[cur.fi][cur.se][t];
            q.push({dist[x][y][i], c - 1, i, x, y});
          }
        } else
          fl = true;
      }
      if (t != i or fl) {
        if (dist[x][y][i] > dist[cur.fi][cur.se][t] + 1) {
          dist[x][y][i] = dist[cur.fi][cur.se][t] + 1;
          q.push({dist[x][y][i], k - 1, i, x, y});
        }
      }
    }
  }
  int ans = OO;
  for (int i = 0; i < 4; i++)
    ans = min(ans, dist[e.fi][e.se][i]);
  if (ans == OO)
    ans = -1;
  cout << ans << '\\n';

  return 0;
}