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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 5;
const int MAXM = 1e6 + 5;
const int MAXP = 1.5e6 + 5;

inline int Abs(int x) { return x < 0 ? -x : x; }

/*
 0
1 3
 2
*/

string s[MAXN];
vector<int> dis[MAXN];

struct Node {
  int x, y;
};

set<int> r[MAXN], c[MAXM];
queue<Node> q;

inline void push(int x, int y) {
  r[x].erase(y);
  c[y].erase(x);
  q.push((Node){x, y});
}

int main(void) {
  int n, m, d;
  scanf("%d%d%d", &n, &m, &d);
  int sx, sy, ex, ey;
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; ++i)
    cin >> s[i], s[i] = '@' + s[i] + '@';

  for (int i = 1; i <= n; ++i) {
    dis[i].resize(m + 1);
    for (int j = 1; j <= m; ++j)
      dis[i][j] = -1;
  }

  for (int i = 0; i <= n + 1; ++i)
    for (int j = 0; j <= m + 1; ++j)
      r[i].insert(j), c[j].insert(i);

  dis[sx][sy] = 0;
  push(sx, sy);

  while (!q.empty()) {
    int x = q.front().x, y = q.front().y;
    q.pop();

    set<int>::iterator it;

    static vector<Node> tmp;
    tmp.clear();

    it = r[x].lower_bound(y);
    while (1) {
      int yy = *it;
      if (yy > m)
        break;
      if (s[x][yy] == '@')
        break;
      if (yy - y > d)
        break;

      dis[x][yy] = dis[x][y] + 1;
      tmp.push_back((Node){x, yy});
      ++it;
    }

    it = r[x].lower_bound(y);
    --it;
    while (1) {
      int yy = *it;
      if (yy < 1)
        break;
      if (s[x][yy] == '@')
        break;
      if (y - yy > d)
        break;

      dis[x][yy] = dis[x][y] + 1;
      tmp.push_back((Node){x, yy});
      --it;
    }

    it = c[y].lower_bound(x);
    while (1) {
      int xx = *it;
      if (xx > n)
        break;
      if (s[xx][y] == '@')
        break;
      if (xx - x > d)
        break;

      dis[xx][y] = dis[x][y] + 1;
      tmp.push_back((Node){xx, y});
      ++it;
    }

    it = c[y].lower_bound(x);
    --it;
    while (1) {
      int xx = *it;
      if (xx < 1)
        break;
      if (s[xx][y] == '@')
        break;
      if (x - xx > d)
        break;

      dis[xx][y] = dis[x][y] + 1;
      tmp.push_back((Node){xx, y});
      --it;
    }

    for (int i = 0; i < (int)tmp.size(); ++i)
      push(tmp[i].x, tmp[i].y);
  }

  printf("%d", dis[ex][ey]);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 5;
const int MAXM = 1e6 + 5;
const int MAXP = 1.5e6 + 5;

inline int Abs(int x) { return x < 0 ? -x : x; }

struct Node {
  int x, y;
  Node *nxt[4];
#define opp_dir(x) ((x) ^ 2)

  void del(void) {
    for (int i = 0; i < 4; ++i)
      nxt[i]->nxt[opp_dir(i)] = nxt[opp_dir(i)];
  }
} p[MAXP];
int pcnt = 0;

/*
 0
1 3
 2
*/

string s[MAXN];
vector<int> dis[MAXN];
vector<Node *> to[MAXN];

int main(void) {
  int n, m, d;
  scanf("%d%d%d", &n, &m, &d);
  int sx, sy, ex, ey;
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; ++i)
    cin >> s[i], s[i] = '@' + s[i] + '@';

  for (int i = 1; i <= n; ++i) {
    dis[i].resize(m + 1);
    for (int j = 1; j <= m; ++j)
      dis[i][j] = -1;
  }

  for (int i = 0; i <= n + 1; ++i) {
    to[i].resize(m + 2);
    for (int j = 0; j <= m + 1; ++j) {
      to[i][j] = &p[++pcnt];
      p[pcnt].x = i;
      p[pcnt].y = j;
    }
  }

  for (int i = 0; i <= n + 1; ++i)
    for (int j = 0; j <= m + 1; ++j) {
      Node *p = to[i][j];
      if (i >= 1)
        p->nxt[0] = to[i - 1][j];
      if (j >= 1)
        p->nxt[1] = to[i][j - 1];
      if (i <= n)
        p->nxt[2] = to[i + 1][j];
      if (j <= m)
        p->nxt[3] = to[i][j + 1];
    }

  dis[sx][sy] = 0;

  static queue<Node *> q;

  q.push(to[sx][sy]);
  to[sx][sy]->del();

  while (!q.empty()) {
    Node *p = q.front();
    q.pop();
    int pos[2] = {p->x, p->y};

    Node *far[4];
    static vector<Node *> tmp[4];

    for (int k = 0; k < 4; ++k) {
      Node *now = p->nxt[k];
      tmp[k].clear();
      while (1) {
        int x = now->x, y = now->y;
        if (x < 1 || x > n)
          break;
        if (y < 1 || y > m)
          break;
        if (s[x][y] == '@')
          break;

        int pp[2] = {x, y};
        if (Abs(pp[k & 1] - pos[k & 1]) > d)
          break;

        dis[x][y] = dis[pos[0]][pos[1]] + 1;
        tmp[k].push_back(now);
        now->del();

        now = now->nxt[k];
      }
      far[k] = now;
    }

    for (int k = 0; k < 4; ++k) {
      far[k]->nxt[opp_dir(k)] = far[opp_dir(k)];
      for (int i = 0; i < (int)tmp[k].size(); ++i) {
        tmp[k][i]->nxt[k] = far[k];
        tmp[k][i]->nxt[opp_dir(k)] = far[opp_dir(k)];

        q.push(tmp[k][i]);
      }
    }
  }

  printf("%d", dis[ex][ey]);
  return 0;
}