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

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #define int long long
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 // 998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp, n, m)                                                      \\
  for (int i = 0; i <= n; i++) {                                               \\
    for (int j = 0; j <= m; j++)                                               \\
      cout << dp[i][j] << " ";                                                 \\
    cout << "\\n";                                                              \\
  }
typedef std::numeric_limits<double> dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const long long N = 1000005, INF = (int)1e9;
lld pi = 3.1415926535897932;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int lcm(int a, int b) {
  int g = __gcd(a, b);
  return a / g * b;
}
int power(int a, int b, int p) {
  if (a == 0)
    return 0;
  int res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}
string s[N];
int n, m, k;
bool check(int x, int y) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] == '@')
    return false;
  return true;
}
int32_t main() {
  IOS;
  cin >> n >> m >> k;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  deque<pair<pii, pair<int, pii>>> q; // x, y, dir, dist(for k)
  x1--;
  y1--;
  x2--;
  y2--;
  q.push_back({{x1, y1}, {0, {0, 1}}});
  q.push_back({{x1, y1}, {1, {0, 1}}});
  q.push_back({{x1, y1}, {2, {0, 1}}});
  q.push_back({{x1, y1}, {3, {0, 1}}});
  int dp[n][m][4];
  rep(i, 0, n) rep(j, 0, m) rep(rr, 0, 4) dp[i][j][rr] = INF;
  dp[x1][y1][0] = 1;
  dp[x1][y1][1] = 1;
  dp[x1][y1][2] = 1;
  dp[x1][y1][3] = 1;
  while (!q.empty()) {
    pair<pii, pair<int, pii>> p = q.front();
    q.pop_front();
    int x, y, dir, dist, cur;
    x = p.ff.ff;
    y = p.ff.ss;
    dir = p.ss.ff;
    dist = p.ss.ss.ff;
    cur = p.ss.ss.ss;
    // cout<<x<<" "<<y<<" "<<dir<<" "<<dist<<" "<<dp[x][y][dir]<<"\\n";
    // cout<<x<<" "<<y<<" "<<dir<<" "<<dist<<"\\n";
    for (int i = 0; i < 4; i++) {
      int xn = x + dx[i], yn = y + dy[i];
      if (check(xn, yn)) {
        if (i == dir && dist < k) {
          if (dp[xn][yn][i] > cur)
            q.push_front({{xn, yn}, {i, {dist + 1, cur}}});
          dp[xn][yn][i] = min(dp[xn][yn][i], cur);
          // cout<<x<<" "<<y<<" "<<dir<<" "<<i<<" "<<xn<<' '<<yn<<'\\n';
        } else {
          //&&(dp[x][y][dir]<min({dp[xn][yn][0], dp[xn][yn][1], dp[xn][yn][2],
          //dp[xn][yn][3]})))
          if (dp[xn][yn][i] > (cur + 1))
            q.push_back({{xn, yn}, {i, {1, cur + 1}}});
          // if(xn==2&&yn==1&&i==1)
          // cout<<x<<" "<<y<<" "<<dir<<" "<<dist<<" "<<dp[x][y][dir]<<"yo\\n";
          dp[xn][yn][i] = min(dp[xn][yn][i], cur + 1);
        }
      }
    }
  }
  // cout<<dp[2][1][1]<<' '<<dp[1][1][1]<<"\\n";
  /**for(int i=0;i<n;i++)
  {
      for(int j=0;j<m;j++)
      {
          cout<<dp[i][j][0]<<dp[i][j][1]<<" ";
      }
      cout<<"\\n";
      for(int j=0;j<m;j++)
      {
          cout<<dp[i][j][2]<<dp[i][j][3]<<" ";
      }
      cout<<"\\n\\n";
  }**/
  int ans = INF;
  for (int i = 0; i < 4; i++)
    ans = min(ans, dp[x2][y2][i]);
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
}


Error: -11

Fixed submission:

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #define int long long
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 // 998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp, n, m)                                                      \\
  for (int i = 0; i <= n; i++) {                                               \\
    for (int j = 0; j <= m; j++)                                               \\
      cout << dp[i][j] << " ";                                                 \\
    cout << "\\n";                                                              \\
  }
typedef std::numeric_limits<double> dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const long long N = 1000005, INF = (int)1e9;
lld pi = 3.1415926535897932;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int lcm(int a, int b) {
  int g = __gcd(a, b);
  return a / g * b;
}
int power(int a, int b, int p) {
  if (a == 0)
    return 0;
  int res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}
string s[N];
int n, m, k;
bool check(int x, int y) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] == '@')
    return false;
  return true;
}
int32_t main() {
  IOS;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  deque<pair<pii, pii>> q; // x, y, dir, dist(for k)
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  q.push_back({{x1, y1}, {0, 0}});
  q.push_back({{x1, y1}, {1, 0}});
  q.push_back({{x1, y1}, {2, 0}});
  q.push_back({{x1, y1}, {3, 0}});
  int dp[n][m][4];
  rep(i, 0, n) rep(j, 0, m) rep(k, 0, 4) dp[i][j][k] = INF;
  dp[x1][y1][0] = 1;
  dp[x1][y1][1] = 1;
  dp[x1][y1][2] = 1;
  dp[x1][y1][3] = 1;
  while (!q.empty()) {
    pair<pii, pii> p = q.front();
    q.pop_front();
    int x, y, dir, dist;
    x = p.ff.ff;
    y = p.ff.ss;
    dir = p.ss.ff;
    dist = p.ss.ss;
    // cout<<x<<" "<<y<<" "<<dir<<" "<<dist<<"\\n";
    for (int i = 0; i < 4; i++) {
      int xn = x + dx[i], yn = y + dy[i];
      if (check(xn, yn)) {
        if (i == dir && dist < k) {
          if (dp[xn][yn][i] == INF)
            q.push_front({{xn, yn}, {i, dist + 1}});
          dp[xn][yn][i] = min(dp[xn][yn][i], dp[x][y][dir]);
        } else {
          if (dp[xn][yn][i] == INF)
            q.push_back({{xn, yn}, {i, 1}});
          dp[xn][yn][i] = min(dp[xn][yn][i], dp[x][y][dir] + 1);
        }
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < 4; i++)
    ans = min(ans, dp[x2][y2][i]);
  if (x1 == x2 && y1 == y2) {
    cout << 0;
    return 0;
  }
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
}