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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define loop(k) rep(author_is_masa, k)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18 + 10;

/*--------------------------------------------------------------------*/

signed main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  deque<pii> q;
  q.push_front(make_pair(ch, cw));
  vector<vi> added(h, vi(w, 0));

  vector<vi> dist(h, vi(w, INF));
  dist[ch][cw] = 0;

  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    int x = p.first;
    int y = p.second;
    // cout<<x<<' '<<y<<' '<<dist[x][y]<<endl;

    // rep(i,4){
    // 	int nx=x+dx[i];
    // 	int ny=y+dy[i];
    // 	chmin(dist[x][y],dist[nx][ny]);
    // }

    int cnt = 0;
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx > h - 1 || ny < 0 || ny > w - 1)
        continue;
      // if(dist[nx][ny]<INF)continue;
      if (s[nx][ny] == '#')
        continue;
      if (added[nx][ny] > 1)
        continue;
      chmin(dist[nx][ny], dist[x][y]);
      q.push_front(make_pair(nx, ny));
      added[nx][ny]++;
      cnt++;
    }
    if (cnt == 4)
      continue;
    FOR(i, -2, 2) {
      FOR(j, -2, 2) {

        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx > h - 1 || ny < 0 || ny > w - 1)
          continue;
        if (s[nx][ny] == '#')
          continue;
        // if(dist[nx][ny]<INF) continue;
        if (added[nx][ny] > 0)
          continue;

        chmin(dist[nx][ny], dist[x][y] + 1);

        q.push_back(make_pair(nx, ny));
        added[nx][ny] = 1;
      }
    }
  }

  if (dist[dh][dw] < INF) {
    cout << dist[dh][dw] << endl;
  } else {
    cout << -1 << endl;
  }

  // rep(i,h){
  // 	rep(j,w){
  // 		if(j>0)cout<<' ';
  // 		cout<<dist[i][j];
  // 	}
  // 	cout<<endl;
  // }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define loop(k) rep(author_is_masa, k)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18 + 10;

/*--------------------------------------------------------------------*/

signed main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  queue<pii> q;
  queue<pii> que2;
  q.push(make_pair(ch, cw));

  vector<vi> dist(h, vi(w, INF));
  dist[ch][cw] = 0;

  FOR(memo, 1, 1e5) {
    int cnt = 0;
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int x = p.first;
      int y = p.second;

      rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || nx > h - 1 || ny < 0 || ny > w - 1)
          continue;
        if (dist[nx][ny] < INF)
          continue;

        // printf("1,%d,%d\\n",nx,ny);

        if (s[nx][ny] == '#') { // 壁の隣
          que2.push(make_pair(x, y));
          break;
        }
        chmin(dist[nx][ny], dist[x][y]);
        cnt++;
        q.push(make_pair(nx, ny));
      }
    }

    // printf("que2.empty=%d\\n",que2.empty());
    while (!que2.empty()) {
      auto p = que2.front();
      que2.pop();
      int x = p.first;
      int y = p.second;

      FOR(i, -2, 2) {
        FOR(j, -2, 2) {

          int nx = x + i;
          int ny = y + j;
          if (nx < 0 || nx > h - 1 || ny < 0 || ny > w - 1)
            continue;
          if (s[nx][ny] == '#')
            continue;
          if (dist[nx][ny] < INF)
            continue;

          // printf("2,%d,%d\\n",nx,ny);

          chmin(dist[nx][ny], dist[x][y] + 1);
          cnt++;
          q.push(make_pair(nx, ny));
        }
      }
    }

    if (cnt == 0) {
      rep(i, h) {
        rep(j, w) {
          if (j > 0)
            cout << ' ';
          cout << dist[i][j];
        }
        cout << endl;
      }
      if (dist[dh][dw] < INF) {
        cout << dist[dh][dw] << endl;
      } else {
        assert(0);
        cout << -1 << endl;
      }
      return 0;
    }
  }

  return 0;
}