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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

const int MAXN = 1e6 + 10;
const int INF = 1e6;

int H, W;
vector<int> aa[MAXN];
vector<int> pp[MAXN];

bool isinside(int i, int j) {
  if (i < 0 or i >= H)
    return false;
  if (j < 0 or j >= W)
    return false;
  return true;
}

int tonum(int i, int j) { return i * W + j; }

void amica(int u, int v, int p) {
  aa[u].push_back(v);
  pp[u].push_back(p);
  aa[v].push_back(u);
  pp[v].push_back(p);
}

vector<pair<int, int>> dd = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int bfs(int start, int end) {
  //~ cout << start << " " << end << endl;
  vector<int> dist(H * W, INF);
  vector<bool> done(H * W, false);
  priority_queue<pair<int, int>> bfs;
  bfs.push({0, start});
  dist[start] = 0;
  while (!bfs.empty()) {
    int u = bfs.top().second;
    bfs.pop();
    if (done[u])
      continue;
    done[u] = true;
    //~ cout << u << " " << dist[u] << endl;
    for (int i = 0; i < (int)aa[u].size(); i++) {
      int a = aa[u][i], p = pp[u][i];
      //~ cout << u << " " << a << " " << p << endl;
      if (dist[a] > dist[u] + p) {
        dist[a] = dist[u] + p;
        bfs.push({-dist[a], a});
      }
    }
  }
  if (dist[end] == INF)
    return -1;
  return dist[end];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  cin >> H >> W;
  int Cx, Cy, Dx, Dy;
  cin >> Cx >> Cy >> Dx >> Dy;
  Cx--, Cy--, Dx--, Dy--;
  vector<vector<char>> grid(H, vector<char>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> grid[i][j];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (grid[i][j] == '#')
        continue;
      int u = tonum(i, j);
      for (auto pp : dd) {
        int di = pp.first, dj = pp.second;
        if (!isinside(i + di, j + dj))
          continue;
        if (grid[i + di][j + dj] == '#')
          continue;
        int v = tonum(i + di, j + dj);
        amica(u, v, 0);
      }
      for (int di = -2; di <= 2; di++)
        for (int dj = -2; dj <= 2; dj++) {
          if (di == 0)
            if (dj == 1 or dj == -1)
              continue;
          if (dj == 0)
            if (di == 1 or di == -1)
              continue;
          if (!isinside(i + di, j + dj))
            continue;
          if (grid[i + di][j + dj] == '#')
            continue;
          int v = tonum(i + di, j + dj);
          amica(u, v, 1);
        }
    }
  }
  cout << bfs(tonum(Cx, Cy), tonum(Dx, Dy)) << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

const int MAXN = 1000 + 10;
const int INF = 1e6;

int H, W;
vector<int> aa[MAXN];
vector<int> pp[MAXN];

bool isinside(int i, int j) {
  if (i < 0 or i >= H)
    return false;
  if (j < 0 or j >= W)
    return false;
  return true;
}

int tonum(int i, int j) { return i * W + j; }

void amica(int u, int v, int p) {
  aa[u].push_back(v);
  pp[u].push_back(p);
  aa[v].push_back(u);
  pp[v].push_back(p);
}

vector<pair<int, int>> dd = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int bfs(int start, int end) {
  //~ cout << start << " " << end << endl;
  vector<int> dist(H * W, INF);
  vector<bool> done(H * W, false);
  priority_queue<pair<int, int>> bfs;
  bfs.push({0, start});
  dist[start] = 0;
  while (!bfs.empty()) {
    int u = bfs.top().second;
    bfs.pop();
    if (done[u])
      continue;
    done[u] = true;
    //~ cout << u << " " << dist[u] << endl;
    for (int i = 0; i < (int)aa[u].size(); i++) {
      int a = aa[u][i], p = pp[u][i];
      //~ cout << u << " " << a << " " << p << endl;
      if (dist[a] > dist[u] + p) {
        dist[a] = dist[u] + p;
        bfs.push({-dist[a], a});
      }
    }
  }
  if (dist[end] == INF)
    return -1;
  return dist[end];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  cin >> H >> W;
  int Cx, Cy, Dx, Dy;
  cin >> Cx >> Cy >> Dx >> Dy;
  Cx--, Cy--, Dx--, Dy--;
  vector<vector<char>> grid(H, vector<char>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> grid[i][j];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (grid[i][j] == '#')
        continue;
      int u = tonum(i, j);
      for (auto pp : dd) {
        int di = pp.first, dj = pp.second;
        if (!isinside(i + di, j + dj))
          continue;
        if (grid[i + di][j + dj] == '#')
          continue;
        int v = tonum(i + di, j + dj);
        amica(u, v, 0);
      }
      for (int di = -2; di <= 2; di++)
        for (int dj = -2; dj <= 2; dj++) {
          if (!isinside(i + di, j + dj))
            continue;
          if (grid[i + di][j + dj] == '#')
            continue;
          int v = tonum(i + di, j + dj);
          amica(u, v, 1);
        }
    }
  }
  cout << bfs(tonum(Cx, Cy), tonum(Dx, Dy)) << "\\n";
}