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

/*input
4 4
1 4
4 1
.##.
####
####
.##.
*/

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tgmath.h>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define ar array
#define scd(t) scanf("%d", &t)
#define scld(t) scanf("%ld", &t)
#define sclld(t) scanf("%lld", &t)
#define scc(t) scanf("%c", &t)
#define scs(t) scanf("%s", t)
#define scf(t) scanf("%f", &t)
#define sclf(t) scanf("%lf", &t)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define tests                                                                  \\
  int r;                                                                       \\
  cin >> r;                                                                    \\
  while (r--)
#define MP make_pair
#define PI acos(-1)
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define godspeed                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);

#define M(x) ((x) % (1000000007))

int arr[1010][1010];

int h;
int w;

bool legal(int x, int y) {
  if (x >= 1 && x <= h) {
    if (y >= 1 && y <= w) {
      if (arr[x][y]) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  godspeed;
  int a;
  int b;
  int a1;
  int b1;
  cin >> h >> w >> a >> b >> a1 >> b1;
  rep(i, 1010) {
    rep(j, 1010) { arr[i][j] = 0; }
  }
  rep(i, h) {
    rep(j, w) {
      char g;
      cin >> g;
      if (g == '.') {
        arr[i + 1][j + 1] = 1;
      }
    }
  }
  int dx[4] = {-1, 1, 0, 0};
  int dy[4] = {0, 0, -1, 1};
  int dist[h + 2][w + 2];
  rep(i, h + 2) {
    rep(j, w + 2) { dist[i][j] = 1000000000; }
  }
  dist[a][b] = 0;
  deque<PII> q;
  q.push_front({a, b});
  while (!q.empty()) {
    PII l = q.front();
    q.pop_front();
    int x = l.first;
    int y = l.second;
    rep(i, 4) {
      int curx = x + dx[i];
      int cury = y + dy[i];
      if (legal(curx, cury)) {
        if (dist[curx][cury] > dist[x][y]) {
          dist[curx][cury] = dist[x][y];
          q.push_front({curx, cury});
        }
      }
    }
    for (int g = x - 2; g <= x + 2; g++) {
      for (int p = y - 2; p <= y + 2; p++) {
        if (legal(g, p)) {
          if (dist[g][p] > dist[x][y] + 1) {
            dist[g][p] = dist[x][y] + 1;
            q.push_back({g, p});
          }
        }
      }
    }
  }
  if (dist[a1][b1] > 10000000) {
    cout << -1 << endl;
  } else {
    cout << dist[a1][b1] << endl;
  }
}


Error: 0

Fixed submission:

/*input
4 4
1 4
4 1
.##.
####
####
.##.
*/

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tgmath.h>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define ar array
#define scd(t) scanf("%d", &t)
#define scld(t) scanf("%ld", &t)
#define sclld(t) scanf("%lld", &t)
#define scc(t) scanf("%c", &t)
#define scs(t) scanf("%s", t)
#define scf(t) scanf("%f", &t)
#define sclf(t) scanf("%lf", &t)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define tests                                                                  \\
  int r;                                                                       \\
  cin >> r;                                                                    \\
  while (r--)
#define MP make_pair
#define PI acos(-1)
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define godspeed                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);

#define M(x) ((x) % (1000000007))

int arr[1000][1000];

int h;
int w;

bool legal(int x, int y) {
  if (x >= 1 && x <= h) {
    if (y >= 1 && y <= w) {
      if (arr[x][y]) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  godspeed;
  int a;
  int b;
  int a1;
  int b1;
  cin >> h >> w >> a >> b >> a1 >> b1;
  memset(arr, 0, sizeof(arr));
  rep(i, h) {
    rep(j, w) {
      char g;
      cin >> g;
      if (g == '.') {
        arr[i + 1][j + 1] = 1;
      }
    }
  }
  int dx[4] = {-1, 1, 0, 0};
  int dy[4] = {0, 0, -1, 1};
  int dist[h + 2][w + 2];
  rep(i, h + 2) {
    rep(j, w + 2) { dist[i][j] = 1000000000; }
  }
  dist[a][b] = 0;
  deque<PII> q;
  q.push_front({a, b});
  while (!q.empty()) {
    PII l = q.front();
    q.pop_front();
    int x = l.first;
    int y = l.second;
    rep(i, 4) {
      int curx = x + dx[i];
      int cury = y + dy[i];
      if (legal(curx, cury)) {
        if (dist[curx][cury] > dist[x][y]) {
          dist[curx][cury] = dist[x][y];
          q.push_front({curx, cury});
        }
      }
    }
    for (int g = x - 2; g <= x + 2; g++) {
      for (int p = y - 2; p <= y + 2; p++) {
        if (legal(g, p)) {
          if (dist[g][p] > dist[x][y] + 1) {
            dist[g][p] = dist[x][y] + 1;
            q.push_back({g, p});
          }
        }
      }
    }
  }
  if (dist[a1][b1] > 10000000) {
    cout << -1 << endl;
  } else {
    cout << dist[a1][b1] << endl;
  }
}