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

#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define me0(a) memset(a, 0, sizeof(a))
#define me1(a) memset(a, -1, sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\\\acm\\\\Cproj\\\\in.txt", "r", stdin);
#define pr freopen("C:\\\\acm\\\\Cproj\\\\out.txt", "w", stdout)
#define pr2 freopen("C:\\\\acm\\\\Cproj\\\\std.txt", "w", stdout)
#define pii pair<int, int>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T> void read(T &val) {
  T x = 0;
  T bz = 1;
  char c;
  for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
    ;
  if (c == '-') {
    bz = -1;
    c = getchar();
  }
  for (; c >= '0' && c <= '9'; c = getchar())
    x = x * 10 + c - 48;
  val = x * bz;
}
const int maxn = 1e6 + 11;
const int mod = 1e9 + 7;
int n, m, x, y, t;
char g[1100][1100];
int to(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m || g[x][y] == '#')
    return false;
  return true;
}

bool vis[maxn];
LL dis[maxn];
int tot, head[maxn];
struct Edge {
  int to, nxt, w;
} e[maxn * 100];
void add(int u, int v, int w) {
  //    cout<<"add "<<u<<" "<<v<<" "<<w<<endl;
  e[++tot] = {v, head[u], w};
  head[u] = tot;
}
struct HeapNode {
  LL dis, u;
  bool operator<(const HeapNode b) const { return dis > b.dis; }
};
void dijkstra(int s) {
  me0(vis);
  memset(dis, 0x3f, sizeof(dis));
  dis[s] = 0;
  priority_queue<HeapNode> q;
  q.push({0, s});
  while (!q.empty()) {
    HeapNode tp = q.top();
    q.pop();
    int u = tp.u;
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].nxt) {
      int to = e[i].to;
      if (dis[to] > dis[u] + e[i].w) {
        dis[to] = dis[u] + e[i].w;
        q.push({dis[to], to});
      }
    }
  }
}

int main() {
  //    op;
  read(n);
  read(m);
  int cx, cy, dx, dy;
  read(cx);
  read(cy);
  read(dx);
  read(dy);
  fo(i, 1, n) scanf("%s", g[i] + 1);
  fo(i, 1, n) {
    fo(j, 1, m) {
      int nx = i + 1, ny = j;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      nx = i - 1, ny = j;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      nx = i, ny = j + 1;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      nx = i, ny = j - 1;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      fo(a, -2, 2) {
        fo(b, -2, 2) {
          if (a == 0 && b == 0)
            continue;
          nx = i + a, ny = j + b;
          if (check(nx, ny))
            add(to(i, j), to(nx, ny), 1);
        }
      }
    }
  }

  dijkstra(to(cx, cy));

  if (dis[to(dx, dy)] >= INF)
    puts("-1");
  else
    printf("%d\\n", dis[to(dx, dy)]);

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define me0(a) memset(a, 0, sizeof(a))
#define me1(a) memset(a, -1, sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\\\acm\\\\Cproj\\\\in.txt", "r", stdin);
#define pr freopen("C:\\\\acm\\\\Cproj\\\\out.txt", "w", stdout)
#define pr2 freopen("C:\\\\acm\\\\Cproj\\\\std.txt", "w", stdout)
#define pii pair<int, int>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T> void read(T &val) {
  T x = 0;
  T bz = 1;
  char c;
  for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
    ;
  if (c == '-') {
    bz = -1;
    c = getchar();
  }
  for (; c >= '0' && c <= '9'; c = getchar())
    x = x * 10 + c - 48;
  val = x * bz;
}
const int maxn = 1e6 + 11;
const int mod = 1e9 + 7;
int n, m, x, y, t;
char g[1100][1100];
int to(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m || g[x][y] == '#')
    return false;
  return true;
}

bool vis[maxn];
LL dis[maxn];
int tot, head[maxn];
struct Edge {
  int to, nxt, w;
} e[maxn << 1];
void add(int u, int v, int w) {
  //    cout<<"add "<<u<<" "<<v<<" "<<w<<endl;
  e[++tot] = {v, head[u], w};
  head[u] = tot;
}
struct HeapNode {
  LL dis, u;
  bool operator<(const HeapNode b) const { return dis > b.dis; }
};
void dijkstra(int s) {
  me0(vis);
  memset(dis, 0x3f, sizeof(dis));
  dis[s] = 0;
  priority_queue<HeapNode> q;
  q.push({0, s});
  while (!q.empty()) {
    HeapNode tp = q.top();
    q.pop();
    int u = tp.u;
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].nxt) {
      int to = e[i].to;
      if (dis[to] > dis[u] + e[i].w) {
        dis[to] = dis[u] + e[i].w;
        q.push({dis[to], to});
      }
    }
  }
}

int main() {
  //    op;
  read(n);
  read(m);
  int cx, cy, dx, dy;
  read(cx);
  read(cy);
  read(dx);
  read(dy);
  fo(i, 1, n) scanf("%s", g[i] + 1);
  fo(i, 1, n) {
    fo(j, 1, m) {
      int nx = i + 1, ny = j;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      nx = i - 1, ny = j;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      nx = i, ny = j + 1;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      nx = i, ny = j - 1;
      if (check(nx, ny))
        add(to(i, j), to(nx, ny), 0);
      fo(a, -2, 2) {
        fo(b, -2, 2) {
          if (a == 0 && b == 0)
            continue;
          nx = i + a, ny = j + b;
          if (check(nx, ny))
            add(to(i, j), to(nx, ny), 1);
        }
      }
    }
  }

  dijkstra(to(cx, cy));

  if (dis[to(dx, dy)] >= INF)
    puts("-1");
  else
    printf("%d\\n", dis[to(dx, dy)]);

  return 0;
}