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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<tuple<ll, ll, ll>>>;

// MOD確認

ll dx[4]{-1, 0, 1, 0};
ll dy[4]{0, -1, 0, 1};

ll H, W, K, X1, Y1, X2, Y2;

bool inrange(ll x, ll y) { return 0 <= x && x < H && 0 <= y && y < W; }

/*char field[110][110];
ll d[110][110];*/

int main() {
  /*vector<vector<char>> field(H,vector<char>(W));
  vector<vector<ll>> d(H+10,vector<ll>(W+10));
  vector<vector<ll>> last(H+10,vector<ll>(W+10));*/
  queue<pair<ll, ll>> Q;

  cin >> H >> W >> K >> X1 >> Y1 >> X2 >> Y2;
  FOR(i, 1, 7) {
    if (pow(10, i - 1) <= H && H < pow(10, i)) {
      ll h = pow(10, i) + 10, w = pow(10, 7 - i) + 10;
      vector<vector<char>> field(h, vector<char>(w));
      vector<vector<ll>> d(h, vector<ll>(w));
      vector<vector<ll>> last(h, vector<ll>(w));
      rep(i, H) rep(j, W) cin >> field[i][j];
      rep(i, H) rep(j, W) d[i][j] = INF;
      d[X1 - 1][Y1 - 1] = 0;
      Q.emplace(X1 - 1, Y1 - 1);
      while (Q.size()) {
        ll x, y;
        tie(x, y) = Q.front();
        Q.pop();
        rep(i, 4) {
          FOR(j, 1, K + 1) {
            ll nx = x + dx[i] * j, ny = y + dy[i] * j;
            if (!inrange(nx, ny))
              break;
            if (field[nx][ny] == '@')
              break;
            if (d[nx][ny] <= d[x][y])
              break;
            if (d[nx][ny] > d[x][y] + 1) {
              Q.emplace(nx, ny);
              d[nx][ny] = d[x][y] + 1;
            }
          }
        }
      }
      if (d[X2 - 1][Y2 - 1] == INF)
        cout << -1 << endl;
      else
        cout << d[X2 - 1][Y2 - 1] << endl;
      return 0;
    }
  }

  /*rep(i,H) rep(j,W) cin >> field[i][j];

  rep(i,H) rep(j,W) d[i][j] = INF;
  d[X1-1][Y1-1] = 0;
  Q.emplace(X1-1,Y1-1);

  while(Q.size()){
     ll x,y;
     tie(x,y) = Q.front();Q.pop();
     rep(i,4){
        FOR(j,1,K+1){
           ll nx = x + dx[i] * j,ny = y + dy[i] * j;
           if(!inrange(nx,ny)) continue;
           if(field[nx][ny] == '@' || d[nx][ny] != INF && last[nx][ny] !=
  d[x][y]) break; d[nx][ny] = d[x][y] + 1; last[nx][ny] = d[x][y];
           Q.emplace(nx,ny);
        }
     }
  }

  rep(i,H){
     rep(j,W){
        if(d[i][j] != INF)cout << d[i][j];
        else cout << '#';
     }
     cout << endl;
  }


  if(d[X2-1][Y2-1] == INF) cout << -1 << endl;
  else cout << d[X2-1][Y2-1] << endl;*/
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<tuple<ll, ll, ll>>>;

// MOD確認

ll dx[4]{-1, 0, 1, 0};
ll dy[4]{0, -1, 0, 1};

ll H, W, K, X1, Y1, X2, Y2;

bool inrange(ll x, ll y) { return 0 <= x && x < H && 0 <= y && y < W; }

/*char field[110][110];
ll d[110][110];*/

int main() {
  /*vector<vector<char>> field(H,vector<char>(W));
  vector<vector<ll>> d(H+10,vector<ll>(W+10));
  vector<vector<ll>> last(H+10,vector<ll>(W+10));*/
  queue<pair<ll, ll>> Q;

  cin >> H >> W >> K >> X1 >> Y1 >> X2 >> Y2;
  FOR(i, 1, 7) {
    if (pow(10, i - 1) <= H && H < pow(10, i)) {
      ll h = pow(10, i) + 10, w = pow(10, 6 - i) + 10;
      vector<vector<char>> field(h, vector<char>(w));
      vector<vector<ll>> d(h, vector<ll>(w));
      vector<vector<ll>> last(h, vector<ll>(w));
      rep(i, H) rep(j, W) cin >> field[i][j];
      rep(i, H) rep(j, W) d[i][j] = INF;
      d[X1 - 1][Y1 - 1] = 0;
      Q.emplace(X1 - 1, Y1 - 1);
      while (Q.size()) {
        ll x, y;
        tie(x, y) = Q.front();
        Q.pop();
        rep(i, 4) {
          FOR(j, 1, K + 1) {
            ll nx = x + dx[i] * j, ny = y + dy[i] * j;
            if (!inrange(nx, ny))
              continue;
            if (field[nx][ny] == '@' ||
                d[nx][ny] != INF && last[nx][ny] < d[x][y])
              break;
            d[nx][ny] = d[x][y] + 1;
            last[nx][ny] = d[x][y];
            Q.emplace(nx, ny);
          }
        }
      }
      if (d[X2 - 1][Y2 - 1] == INF)
        cout << -1 << endl;
      else
        cout << d[X2 - 1][Y2 - 1] << endl;
      return 0;
    }
  }

  /*rep(i,H) rep(j,W) cin >> field[i][j];

  rep(i,H) rep(j,W) d[i][j] = INF;
  d[X1-1][Y1-1] = 0;
  Q.emplace(X1-1,Y1-1);

  while(Q.size()){
     ll x,y;
     tie(x,y) = Q.front();Q.pop();
     rep(i,4){
        FOR(j,1,K+1){
           ll nx = x + dx[i] * j,ny = y + dy[i] * j;
           if(!inrange(nx,ny)) continue;
           if(field[nx][ny] == '@' || d[nx][ny] != INF && last[nx][ny] !=
  d[x][y]) break; d[nx][ny] = d[x][y] + 1; last[nx][ny] = d[x][y];
           Q.emplace(nx,ny);
        }
     }
  }

  rep(i,H){
     rep(j,W){
        if(d[i][j] != INF)cout << d[i][j];
        else cout << '#';
     }
     cout << endl;
  }


  if(d[X2-1][Y2-1] == INF) cout << -1 << endl;
  else cout << d[X2-1][Y2-1] << endl;*/
}