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

#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
#define endl "\\n"
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define ALL(x) (x).begin(), (x).end()
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll mod = 1e9 + 7;
constexpr int NIL = -1;
template <class T> vector<T> vec(int len, T elem) {
  return vector<T>(len, elem);
} // auto dp = vec(52, vec(103, vec(103, INF)));
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline int popcount(T a) { return __builtin_popcount(a); }
template <class T> inline T emod(T a, T p) { return (a % p + p) % p; }
//-------------------
int h, w;
vector<string> s;
constexpr int MX = 3005;

int V;
struct Edge {
  int to;
  int cost;
};
vector<Edge> G[MX * MX];
int d[MX * MX];

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

void dijkstra(int s) {
  priority_queue<PII, vector<PII>, greater<PII>> que;
  fill(d, d + V, INF);
  que.push({0, s});

  while (!que.empty()) {
    PII p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] <= p.first)
      continue;
    d[v] = p.first;
    for (auto e : G[v]) {
      if (d[e.to] > d[v] + e.cost) { // make code faster
        que.push({d[v] + e.cost, e.to});
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  cin >> h >> w;

  int c[2];
  cin >> c[0] >> c[1];
  c[0]--;
  c[1]--;
  int cc[2];
  cin >> cc[0] >> cc[1];
  cc[0]--;
  cc[1]--;

  s.assign(h, "");
  rep(i, h) { cin >> s[i]; }

  auto onfield = [&](int y, int x) {
    bool ans = true;
    if (y < 0 or h <= y)
      ans = false;
    if (x < 0 or w <= x)
      ans = false;
    return ans;
  };
  rep(i, h) {
    rep(j, w) {
      // cout << num[i][j] << " ";

      rep(k, 4) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (onfield(ni, nj) and s[ni][nj] == '.') {
          G[i * w + j].push_back({ni * w + nj, 0});
        }
      }
      for (int di = -2; di <= 2; di++) {
        for (int dj = -2; dj <= 2; dj++) {
          int ni = i + di;
          int nj = j + dj;
          if (onfield(ni, nj) and s[ni][nj] == '.') {
            G[i * w + j].push_back({ni * w + nj, 1});
          }
        }
      }
    }
    // cout << endl;
  }

  V = h * w;
  dijkstra(c[0] * w + c[1]);

  int goal = cc[0] * w + cc[1];
  if (d[goal] != INF)
    cout << d[goal] << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
#define endl "\\n"
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define ALL(x) (x).begin(), (x).end()
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll mod = 1e9 + 7;
constexpr int NIL = -1;
template <class T> vector<T> vec(int len, T elem) {
  return vector<T>(len, elem);
} // auto dp = vec(52, vec(103, vec(103, INF)));
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline int popcount(T a) { return __builtin_popcount(a); }
template <class T> inline T emod(T a, T p) { return (a % p + p) % p; }
//-------------------
int h, w;
vector<string> s;
constexpr int MX = 3005;

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

int num[MX][MX];
bool visited[MX][MX];

// void dfs(int y, int x, vector<vector<bool>> &visited, vector<vector<int>>
// &dp) {
//     auto onfield = [&](int y, int x) {
//         bool ans = true;
//         if(y < 0 or h <= y) ans = false;
//         if(x < 0 or w <= x) ans = false;
//         return ans;
//     };
//     if(visited[y][x]) return;
//     visited[y][x] = true;
//     rep(i, 4) {
//         int ny = y + dy[i];
//         int nx = x + dx[i];
//         if(onfield(ny, nx) and s[ny][nx] == '.') {
//             chmin(dp[ny][nx], dp[y][x]);
//             dfs(ny, nx, visited, dp);
//         }
//     }
//     for(int i=-2; i<=2; i++) {
//         for(int j=-2; j<=2; j++) {
//             int ny = y + i;
//             int nx = x + j;
//             if(onfield(ny, nx) and s[ny][nx] == '.') {
//                 chmin(dp[ny][nx], dp[y][x]+1);
//                 dfs(ny, nx, visited, dp);
//             }
//         }
//     }
//  }

bool dfs(int y, int x, int cur) {
  auto onfield = [&](int y, int x) {
    bool ans = true;
    if (y < 0 or h <= y)
      ans = false;
    if (x < 0 or w <= x)
      ans = false;
    return ans;
  };
  if (visited[y][x] or s[y][x] == '#')
    return false;
  visited[y][x] = true;
  num[y][x] = cur;
  rep(i, 4) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (onfield(ny, nx) and s[ny][nx] == '.') {

      dfs(ny, nx, cur);
    }
  }
  return true;
}

vector<int> d(MX, INF);

void dijkstra(int s, vector<vector<int>> &G, int V) {
  priority_queue<PII, vector<PII>, greater<PII>> que;
  que.push({0, s});

  while (!que.empty()) {
    PII p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] <= p.first)
      continue;
    d[v] = p.first;
    // cout << "OK"<< endl;
    rep(i, V) {
      if (G[v][i] == 0 or i == v)
        continue;
      // cout << i << endl;
      if (d[i] > d[v] + 1) { // make code faster
        que.push({d[v] + 1, i});
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  cin >> h >> w;

  int c[2];
  cin >> c[0] >> c[1];
  c[0]--;
  c[1]--;
  int cc[2];
  cin >> cc[0] >> cc[1];
  cc[0]--;
  cc[1]--;

  s.assign(h, "");
  rep(i, h) { cin >> s[i]; }

  rep(i, h) rep(j, w) num[i][j] = -1;
  int cnt = 0;
  rep(i, h) {
    rep(j, w) {
      if (dfs(i, j, cnt))
        cnt++;
    }
  }
  // cout << cnt << endl;
  vector<vector<int>> G;
  G.assign(cnt, vector<int>(cnt, 0));

  auto onfield = [&](int y, int x) {
    bool ans = true;
    if (y < 0 or h <= y)
      ans = false;
    if (x < 0 or w <= x)
      ans = false;
    return ans;
  };
  rep(i, h) {
    rep(j, w) {
      // cout << num[i][j] << " ";
      for (int di = -2; di <= 2; di++) {
        for (int dj = -2; dj <= 2; dj++) {
          int ni = i + di;
          int nj = j + dj;
          if (onfield(ni, nj) and s[ni][nj] == '.' and num[i][j] >= 0 and
              num[ni][nj] >= 0) {
            G[num[i][j]][num[ni][nj]] = 1;
            G[num[ni][nj]][num[i][j]] = 1;
          }
        }
      }
    }
    // cout << endl;
  }

  // rep(i, cnt) rep(j, cnt) cout << G[i][j] << endl;

  dijkstra(num[c[0]][c[1]], G, cnt);
  // rep(i, h) {
  //     rep(j, w) cout << dp[i][j] << " ";
  //     cout << endl;
  // }

  // rep(i, cnt) cout << d[i] << endl;

  if (d[num[cc[0]][cc[1]]] != INF)
    cout << d[num[cc[0]][cc[1]]] << endl;
  else
    cout << -1 << endl;
  return 0;
}