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

#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define SET(x, y) x |= (1 << y)
#define CLEAR(x, y) x &= ~(1 << y)
#define READ(x, y) ((0u == (x & (1 << y))) ? 0u : 1u)
#define TOGGLE(x, y) (x ^= (1 << y))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define si(n) scanf("%d", &n)
#define sf(n) scanf("%f", &n)
#define sl(n) scanf("%lld", &n)
#define slu(n) scanf("%llu", &n)
#define sd(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define pnl printf("\\n")
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pF first
#define pS second
#define CL(a, b) memset(a, b, sizeof(a))
#define sqr(x) ((x) * (x))
#define intLIMIT numeric_limits<int>
#define longLIMIT numeric_limits<long long>
#define GCD(a, b) __gcd((a), (b))
#define dbl(x) (double)(x)
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vll vector<long long>
#define allof(x) (x).begin(), (x).end()
#define fastCin                                                                \\
  cin.sync_with_stdio(0);                                                      \\
  cin.tie(0)
#define scanUnsigned(x)                                                        \\
  do {                                                                         \\
    while ((x = getchar()) < '0')                                              \\
      ;                                                                        \\
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \\
      ;                                                                        \\
  } while (0)
#define printArr(a, l)                                                         \\
  cout << #a << ": ";                                                          \\
  for (int i = 0; i < (l); i++) {                                              \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define print2dArr(a, r, c)                                                    \\
  cout << #a << ":\\n";                                                         \\
  for (int i = 0; i < (r); i++) {                                              \\
    for (int j = 0; j < (c); j++) {                                            \\
      cout << a[i][j] << " ";                                                  \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
char _;
using namespace std;
void PRINT(int x, int y) {
  for (int i = y - 1; i >= 0; i--) {
    cout << READ(x, i);
  }
  cout << endl;
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef LOCAL_DBG
#define dbg(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define dbg(...) 42
#endif

vector<vector<pii>> g(1500005);

int h, w;
int ch, cw, dh, dw;

void input() {
  char grid[1005][1005] = {0};
  for (int x = 1; x <= h; x++) {
    for (int i = 1; i <= w; i++) {
      cin >> grid[x][i];
    }
  }
  for (int x = 1; x <= h; x++) {
    for (int i = 1; i <= w; i++) {
      if (grid[x][i] != '.') {
        continue;
      }
      if (x + 1 <= h && grid[x + 1][i] == '.') {
        int f = w * x + i, s = w * (x + 1) + i;
        g[f].pb({s, 0});
        g[s].pb({f, 0});
      }
      if (i + 1 <= w && grid[x][i + 1] == '.') {
        int f = w * x + i, s = w * x + i + 1;
        g[f].pb({s, 0});
        g[s].pb({f, 0});
      }
      for (int j = max(x - 2, 1); j <= min(h, x + 2); j++) {
        for (int k = i; k <= min(w, i + 2); k++) {
          if (grid[j][k] == '.') {
            int f = w * x + i, s = w * j + k;
            g[f].pb({s, 1});
            g[s].pb({f, 1});
          }
        }
      }
    }
  }
}

int main() {
  fastCin;
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  input();
  deque<pii> q;
  int v[1500005] = {0};
  bool v2[1500005] = {0};
  CL(v, -1);
  q.pb({ch * w + cw, 0});
  v[ch * w + cw] = 0;
  while (!q.empty()) {
    int cur = q.front().pF, cst = v[q.front().pF];
    q.pop_front();
    if (!v2[cur]) {
      v2[cur] = true;
      for (int x = 0; x < g[cur].size(); x++) {
        int nxt = g[cur][x].pF, cst2 = g[cur][x].pS;
        if (v[nxt] == -1 || cst + cst2 < v[nxt]) {
          v[nxt] = cst + cst2;
          if (cst2 == 0) {
            q.push_front({nxt, cst + cst2});
          } else {
            q.pb({nxt, cst + cst2});
          }
        }
      }
    }
  }
  // for (int x = 1; x <= h; x++) {
  // 	for (int i = 1; i <= w; i++) {
  // 		cout << v[x*w+i] << "\\t";
  // 	} cout << "\\n";
  // } cout << "\\n";
  cout << v[dh * w + dw] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define SET(x, y) x |= (1 << y)
#define CLEAR(x, y) x &= ~(1 << y)
#define READ(x, y) ((0u == (x & (1 << y))) ? 0u : 1u)
#define TOGGLE(x, y) (x ^= (1 << y))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define si(n) scanf("%d", &n)
#define sf(n) scanf("%f", &n)
#define sl(n) scanf("%lld", &n)
#define slu(n) scanf("%llu", &n)
#define sd(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define pnl printf("\\n")
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pF first
#define pS second
#define CL(a, b) memset(a, b, sizeof(a))
#define sqr(x) ((x) * (x))
#define intLIMIT numeric_limits<int>
#define longLIMIT numeric_limits<long long>
#define GCD(a, b) __gcd((a), (b))
#define dbl(x) (double)(x)
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vll vector<long long>
#define allof(x) (x).begin(), (x).end()
#define fastCin                                                                \\
  cin.sync_with_stdio(0);                                                      \\
  cin.tie(0)
#define scanUnsigned(x)                                                        \\
  do {                                                                         \\
    while ((x = getchar()) < '0')                                              \\
      ;                                                                        \\
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \\
      ;                                                                        \\
  } while (0)
#define printArr(a, l)                                                         \\
  cout << #a << ": ";                                                          \\
  for (int i = 0; i < (l); i++) {                                              \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define print2dArr(a, r, c)                                                    \\
  cout << #a << ":\\n";                                                         \\
  for (int i = 0; i < (r); i++) {                                              \\
    for (int j = 0; j < (c); j++) {                                            \\
      cout << a[i][j] << " ";                                                  \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
char _;
using namespace std;
void PRINT(int x, int y) {
  for (int i = y - 1; i >= 0; i--) {
    cout << READ(x, i);
  }
  cout << endl;
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef LOCAL_DBG
#define dbg(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define dbg(...) 42
#endif

char grid[1005][1005] = {0};
vector<vector<pii>> g(1000005);

int main() {
  fastCin;
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  for (int x = 1; x <= h; x++) {
    for (int i = 1; i <= w; i++) {
      cin >> grid[x][i];
    }
  }
  for (int x = 1; x <= h; x++) {
    for (int i = 1; i <= w; i++) {
      if (grid[x][i] != '.') {
        continue;
      }
      if (x + 1 <= h && grid[x + 1][i] == '.') {
        int f = w * x + i, s = w * (x + 1) + i;
        g[f].pb({s, 0});
        g[s].pb({f, 0});
      }
      if (i + 1 <= w && grid[x][i + 1] == '.') {
        int f = w * x + i, s = w * x + i + 1;
        g[f].pb({s, 0});
        g[s].pb({f, 0});
      }
      for (int j = max(x - 2, 1); j <= min(h, x + 2); j++) {
        for (int k = i; k <= min(w, i + 2); k++) {
          if (grid[j][k] == '.') {
            int f = w * x + i, s = w * j + k;
            g[f].pb({s, 1});
            g[s].pb({f, 1});
          }
        }
      }
    }
  }
  deque<pii> q;
  int v[1000005] = {0};
  CL(v, -1);
  q.pb({ch * w + cw, 0});
  int test = 0;
  v[ch * w + cw] = 0;
  while (!q.empty()) {
    test++;
    int cur = q.front().pF, cst = v[q.front().pF];
    q.pop_front();
    // if (v[cur] == -1) {
    // v[cur] = cst;
    for (int x = 0; x < g[cur].size(); x++) {
      int nxt = g[cur][x].pF, cst2 = g[cur][x].pS;
      // if (cur==5 && nxt==6) {
      // 	dbg(cur, nxt, cst, cst2, cst+cst2);
      // }
      if (v[nxt] == -1 || cst + cst2 < v[nxt]) {
        v[nxt] = cst + cst2;
        if (cst2 == 0) {
          q.push_front({nxt, cst + cst2});
        } else {
          q.pb({nxt, cst + cst2});
        }
      }
    }
    // }
  }
  // for (int x = 1; x <= h; x++) {
  // 	for (int i = 1; i <= w; i++) {
  // 		cout << v[x*w+i] << "\\t";
  // 	} cout << "\\n";
  // } cout << "\\n";
  cout << v[dh * w + dw] << "\\n";
  return 0;
}