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

#include <bits/stdc++.h>

#define all(n) n.begin(), n.end()
#define ll long long

#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define pll pair<ll, ll>
#define ff first
#define ss second

using namespace std;
const ll maxn = 2e5 + 20;
const ll maxx = 1e16;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;

ll n, m, k;
ll ax[5] = {-1, 1, 0, 0};
ll ay[5] = {0, 0, -1, 1};
unordered_map<ll, unordered_map<ll, ll>> dd;
unordered_map<ll, unordered_map<ll, ll>> dp;
unordered_map<ll, unordered_map<ll, ll>> dp1;
unordered_map<ll, unordered_map<ll, char>> a;

bool chk(ll x, ll y) {

  if (x < 1 || x > n)
    return false;
  if (y < 1 || y > m)
    return false;
  //  if (dd[x][y]) return false;
  if (a[x][y] == '@')
    return false;
  return true;
}
void dosth(ll x, ll y) {
  queue<pll> q;
  if (chk(x, y))
    q.push(make_pair(x, y));
  dd[x][y] = 1;
  dp[x][y] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    // if ()
    for (int i = 3; i >= 0; i--) {
      for (int j = 1; j <= k; j++) {
        ll x1 = p.ff + ax[i] * j;
        ll y1 = p.ss + ay[i] * j;
        if (!chk(x1, y1))
          break;

        // if (dd[x1][y1]) continue;
        // dd[x1][y1]=1;
        if (dp[x1][y1] > dp[p.ff][p.ss] + 1) {
          dp[x1][y1] = dp[p.ff][p.ss] + 1;
          q.push(make_pair(x1, y1));
        } else if (dp[x1][y1] == dp[p.ff][p.ss] + 1)
          continue;
        else
          break;
        // q.push(make_pair(x1,y1));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("TEAMS.INP", "r")) {
    freopen("TEAMS.INP", "r", stdin);
    freopen("TEAMS.OUT", "w", stdout);
  }
  cin >> n >> m >> k;
  ll x, y, x1, y1;
  cin >> x >> y >> x1 >> y1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char x;
      cin >> x;

      a[i][j] = x;
      dp[i][j] = 1e15;
    }
  }

  dosth(x, y);
  ll h = 1e15;
  if (dp[x1][y1] == 1e15) {
    cout << -1;
    return 0;
  }
  cout << dp[x1][y1];
  /*2 3
  4
  1 1 2 3
  ..@
  ...
  @..*/
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define all(n) n.begin(), n.end()
#define ll long long

#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define pll pair<ll, ll>
#define ff first
#define ss second

using namespace std;
const ll maxn = 2e5 + 20;
const ll maxx = 1e16;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;

ll n, m, k;
ll ax[5] = {0, 1, 0, -1};
ll ay[5] = {1, 0, -1, 0};
unordered_map<ll, unordered_map<ll, ll>> dd;
unordered_map<ll, unordered_map<ll, ll>> dp;
unordered_map<ll, unordered_map<ll, ll>> dp1;
unordered_map<ll, unordered_map<ll, char>> a;

bool chk(ll x, ll y) {

  if (x < 1 || x > n)
    return false;
  if (y < 1 || y > m)
    return false;
  if (dd[x][y])
    return false;
  if (a[x][y] == '@')
    return false;
  return true;
}
void dosth(ll x, ll y) {
  queue<pll> q;
  if (chk(x, y))
    q.push(make_pair(x, y));
  dd[x][y] = 1;
  dp[x][y] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    // if ()
    for (int i = 3; i >= 0; i++) {
      for (int j = 1; j <= k; j++) {
        ll x1 = p.ff + ax[i] * j;
        ll y1 = p.ss + ay[i] * j;
        if (!chk(x1, y1))
          break;
        /* if (dd[x1][y1]&&dp[x1][y1]!=dp[p.ss][p.ff]+1)
         {
             //cout <<"wtf"<<" "<<x1<<" "<<y1<<" "<<p.ss<<" "<<p.ff<<endl;
             break;
         }*/
        // if (dd[x1][y1]) continue;
        dd[x1][y1] = 1;
        dp[x1][y1] = dp[p.ff][p.ss] + 1;
        q.push(make_pair(x1, y1));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("TEAMS.INP", "r")) {
    freopen("TEAMS.INP", "r", stdin);
    freopen("TEAMS.OUT", "w", stdout);
  }
  cin >> n >> m >> k;
  ll x, y, x1, y1;
  cin >> x >> y >> x1 >> y1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char x;
      cin >> x;

      a[i][j] = x;
      dp[i][j] = 1e15;
    }
  }

  dosth(x, y);
  ll h = 1e15;
  if (dp[x1][y1] == 1e15) {
    cout << -1;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp1[i][j] = dp[i][j];
      dp[i][j] = 1e15;
    }
  }
  dd.clear();
  dosth(x1, y1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      h = min(dp1[i][j] + dp[i][j], h);
    }
  }
  cout << h;
  /*2 3
  4
  1 1 2 3
  ..@
  ...
  @..*/
}