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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

const int MAX = 10000000;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/****** define constant end ********/
/****** define variable start ******/
ll h, w;
ll cw, ch, dh, dw;
vector<vector<string>> s;
Graph G;
/******* define variable end *******/

typedef pair<ll, ll> P;
vector<ll> d;

void dijkstra(ll s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (ll i = 0; i < h * w; i++)
    d[i] = INF;
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    for (ll i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int solve() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  s.resize(h, vector<string>(w));
  d.resize(h * w);
  REP(i, h) {
    string tmp;
    cin >> tmp;
    REP(j, w) { s[i][j] = tmp.substr(j, 1); }
  }
  G.resize(h * w);
  REP(i, h) {
    REP(j, w) {
      if (s[i][j] == ".") {
        REP(h_magic, 5) {
          REP(w_magic, 5) {
            if (i + h_magic - 2 < h && i + h_magic - 2 >= 0 &&
                j + w_magic - 2 < w && j + w_magic - 2 >= 0) {
              if (s[i + h_magic - 2][j + w_magic - 2] == ".") {
                if (abs(h_magic - 2) + abs(w_magic - 2) >= 2) {
                  Edge e = {w * (i + h_magic - 2) + (j + w_magic - 2), 1};
                  G[w * i + j].push_back(e);
                } else {
                  Edge e = {w * (i + h_magic - 2) + (j + w_magic - 2), 0};
                  G[w * i + j].push_back(e);
                }
              }
            }
          }
        }
      }
    }
  }
  dijkstra((ch - 1) * w + (cw - 1));
  if (d[(dh - 1) * w + (dw - 1)] == INF)
    cout << -1 << endl;
  else
    cout << d[(dh - 1) * w + (dw - 1)] << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

const int MAX = 10000000;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/****** define constant end ********/
/****** define variable start ******/
ll h, w;
ll cw, ch, dh, dw;
vector<vector<string>> s;
Graph G;
/******* define variable end *******/

typedef pair<ll, ll> P;
vector<ll> d;

void dijkstra(ll s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (ll i = 0; i < h * w; i++)
    d[i] = INF;
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    for (ll i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int solve() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  s.resize(h, vector<string>(w));
  d.resize(h * w);
  REP(i, h) {
    string tmp;
    cin >> tmp;
    REP(j, w) { s[i][j] = tmp.substr(j, 1); }
  }
  G.resize(h * w);
  REP(i, h) {
    REP(j, w) {
      if (s[i][j] == ".") {
        REP(h_magic, 5) {
          REP(w_magic, 5) {
            if (i + h_magic - 2 < h && i + h_magic - 2 >= 0 &&
                j + w_magic - 2 < w && j + w_magic - 2 >= 0) {
              if (s[i + h_magic - 2][j + w_magic - 2] == ".") {
                if (abs(h_magic - 2) + abs(w_magic - 2) >= 2) {
                  Edge e = {h * (i + h_magic - 2) + (j + w_magic - 2), 1};
                  G[w * i + j].push_back(e);
                } else {
                  Edge e = {h * (i + h_magic - 2) + (j + w_magic - 2), 0};
                  G[w * i + j].push_back(e);
                }
              }
            }
          }
        }
      }
    }
  }
  dijkstra((ch - 1) * w + (cw - 1));
  if (d[(dh - 1) * w + (dw - 1)] == INF)
    cout << -1 << endl;
  else
    cout << d[(dh - 1) * w + (dw - 1)] << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}