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

#include <bits/stdc++.h>
using namespace std;

#define pu push_back
#define fi first
#define se second
#define mp make_pair
// #define int long long
#define pii pair<int, int>
#define mm (s + e) / 2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (int i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define sz 200000
int xa[4] = {0, 0, 1, -1};
int ya[4] = {1, -1, 0, 0};
int n, m, k;
int check(int x, int y) {
  if (x >= 0 && y >= 0 && x < n && y < m)
    return 1;
  else
    return 0;
}
signed main() {

  fast cin >> n >> m >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int vis[n + 2][m + 2][4];
  int dist[n + 2][m + 2][4];
  int op[n + 2][m + 2][4];
  int B[n + 2][m + 2];
  memset(vis, 0, sizeof(vis));
  memset(B, 0, sizeof(B));
  memset(op, 0, sizeof(op));
  // cout<<"kk";
  multimap<pii, pair<int, pii>> ss;
  // cout<<"kk";
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char aa;
      cin >> aa;
      if (aa == '.')
        B[i][j] = 1;

      for (int r = 0; r < 4; r++) {
        dist[i][j][r] = 1000000000;
      }
    }
  }
  // cout<<"kk";
  a--;
  b--;
  c--;
  d--;
  // cout<<"io";
  ss.insert(mp(mp(1, -k), mp(0, mp(a, b))));
  ss.insert(mp(mp(1, -k), mp(1, mp(a, b))));
  ss.insert(mp(mp(1, -k), mp(2, mp(a, b))));
  ss.insert(mp(mp(1, -k), mp(3, mp(a, b))));
  // cout<<"ll";
  for (int r = 0; r < 4; r++) {
    dist[a][b][r] = 0;
    op[a][b][r] = k;
  }
  // cout<<"kk";
  while (ss.size() > 0) {

    auto yy = *ss.begin();
    int val = yy.fi.fi;
    int cn = -yy.fi.se;
    int x = yy.se.se.fi;
    int y = yy.se.se.se;

    int dir = yy.se.fi;
    // cout<<x<<" "<<y<<" " <<dir<<" "<<cn<<" "<<val<<endl;
    ss.erase(ss.begin());
    if (vis[x][y][dir] == 1)
      continue;
    vis[x][y][dir] = 1;
    for (int i = 0; i < 4; i++) {
      int aa = x + xa[i];
      int bb = y + ya[i];
      // cout<<aa<<" "<<bb<<" "<<check(aa,bb)<<endl;
      if (check(aa, bb) == 1 && B[aa][bb] == 1) { // cout<<aa<<" "<<bb<<endl;
        int u;
        int vl;
        if (i == dir && cn > 0) {
          u = cn - 1;
          vl = val;
        } else {
          vl = val + 1;
          u = k - 1;
        }
        if (dist[aa][bb][i] > vl ||
            (dist[aa][bb][i] == vl && op[aa][bb][i] < u)) {
          dist[aa][bb][i] = vl;
          op[aa][bb][i] = u;
          ss.insert(mp(mp(vl, -u), mp(i, mp(aa, bb))));
        }
      }
    }
  }

  int ans = 1000000000;
  for (int r = 0; r < 4; r++) {
    ans = min(ans, dist[c][d][r]);
  }
  if (ans >= 100000000)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int, int>
#define mm (s + e) / 2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (int i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define sz 200000
int xa[4] = {0, 0, 1, -1};
int ya[4] = {1, -1, 0, 0};
int n, m, k;
int check(int x, int y) {
  if (x >= 0 && y >= 0 && x < n && y < m)
    return 1;
  else
    return 0;
}
signed main() {

  fast cin >> n >> m >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int vis[n + 2][m + 2][4];
  int dist[n + 2][m + 2][4];
  int op[n + 2][m + 2][4];
  int B[n + 2][m + 2];
  memset(vis, 0, sizeof(vis));
  memset(B, 0, sizeof(B));
  memset(op, 0, sizeof(op));
  // cout<<"kk";
  multimap<pii, pair<int, pii>> ss;
  // cout<<"kk";
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char aa;
      cin >> aa;
      if (aa == '.')
        B[i][j] = 1;

      for (int r = 0; r < 4; r++) {
        dist[i][j][r] = 1000000000;
      }
    }
  }
  // cout<<"kk";
  a--;
  b--;
  c--;
  d--;
  // cout<<"io";
  ss.insert(mp(mp(1, -k), mp(0, mp(a, b))));
  ss.insert(mp(mp(1, -k), mp(1, mp(a, b))));
  ss.insert(mp(mp(1, -k), mp(2, mp(a, b))));
  ss.insert(mp(mp(1, -k), mp(3, mp(a, b))));
  // cout<<"ll";
  for (int r = 0; r < 4; r++) {
    dist[a][b][r] = 0;
  }
  // cout<<"kk";
  while (ss.size() > 0) {

    auto yy = *ss.begin();
    int val = yy.fi.fi;
    int cn = -yy.fi.se;
    int x = yy.se.se.fi;
    int y = yy.se.se.se;

    int dir = yy.se.fi;
    // cout<<x<<" "<<y<<" " <<dir<<" "<<cn<<" "<<val<<endl;
    ss.erase(ss.begin());
    if (vis[x][y][dir] == 1)
      continue;
    vis[x][y][dir] = 1;
    for (int i = 0; i < 4; i++) {
      int aa = x + xa[i];
      int bb = y + ya[i];
      // cout<<aa<<" "<<bb<<" "<<check(aa,bb)<<endl;
      if (check(aa, bb) == 1 && B[aa][bb] == 1) { // cout<<aa<<" "<<bb<<endl;
        int u;
        int vl;
        if (i == dir && cn > 1) {
          u = cn - 1;
          vl = val;
        } else {
          vl = val + 1;
          u = k;
        }
        if (dist[aa][bb][i] > vl ||
            (dist[aa][bb][i] == vl && op[aa][bb][i] < u)) {
          dist[aa][bb][i] = vl;
          op[aa][bb][i] = u;
          ss.insert(mp(mp(vl, -u), mp(i, mp(aa, bb))));
        }
      }
    }
  }

  int ans = 1000000000;
  for (int r = 0; r < 4; r++) {
    ans = min(ans, dist[c][d][r]);
  }
  if (ans >= 100000000)
    cout << -1 << endl;
  else
    cout << ans << endl;
}