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

#include <bits/stdc++.h>
#include <queue>

using namespace std;
typedef long long ll;

char c[1001][1001];
int a[1001][1001];
int h, w;
queue<int> pos, pos_next;

void dfs(int x, int y) {
  if (y + 1 <= w && c[x][y + 1] == '.' &&
      (a[x][y + 1] == 0 || a[x][y + 1] > a[x][y])) {
    a[x][y + 1] = a[x][y];
    pos.push(x * 1001 + y + 1);
    dfs(x, y + 1);
  }
  if (y - 1 >= 1 && c[x][y - 1] == '.' &&
      (a[x][y - 1] == 0 || a[x][y - 1] > a[x][y])) {
    a[x][y - 1] = a[x][y];
    pos.push(x * 1001 + y - 1);
    dfs(x, y - 1);
  }
  if (x + 1 <= h && c[x + 1][y] == '.' &&
      (a[x + 1][y] == 0 || a[x + 1][y] > a[x][y])) {
    a[x + 1][y] = a[x][y];
    pos.push(x * 1001 + 1001 + y);
    dfs(x + 1, y);
  }
  if (x - 1 >= 1 && c[x - 1][y] == '.' &&
      (a[x - 1][y] == 0 || a[x - 1][y] > a[x][y])) {
    a[x - 1][y] = a[x][y];
    pos.push(x * 1001 - 1001 + y);
    dfs(x - 1, y);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j)
      cin >> c[i][j];
  }

  pos.push(ch * 1001 + cw);
  a[ch][cw] = 1;
  int px, py;
  while (!pos.empty()) {
    px = pos.front();
    pos.pop();
    py = px % 1001;
    px = px / 1001;
    if (px == dh && py == dw) {
      break;
    }
    dfs(px, py);
    for (int i = (px - 2 >= 1 ? px - 2 : 1); i <= (px + 2 <= h ? px + 2 : h);
         ++i)
      for (int j = (py - 2 >= 1 ? py - 2 : 1); j <= (py + 2 <= w ? py + 2 : w);
           ++j) {
        if (c[i][j] == '.' && (a[i][j] == 0 || a[i][j] > a[px][py] + 1)) {
          a[i][j] = a[px][py] + 1;
          pos_next.push(i * 1001 + j);
        }
      }
    if (pos.empty()) {
      swap(pos, pos_next);
    }
  }
  cout << a[dh][dw] - 1 << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <queue>

using namespace std;
typedef long long ll;

char c[1001][1001];
int a[1001][1001];
int h, w;
queue<int> pos;

void dfs(int x, int y) {
  if (y + 1 <= w && c[x][y + 1] == '.' && a[x][y + 1] == 0) {
    a[x][y + 1] = a[x][y];
    pos.push(x * 1000 + y + 1);
    dfs(x, y + 1);
  }
  if (y - 1 >= 1 && c[x][y - 1] == '.' && a[x][y - 1] == 0) {
    a[x][y - 1] = a[x][y];
    pos.push(x * 1000 + y - 1);
    dfs(x, y - 1);
  }
  if (x + 1 <= h && c[x + 1][y] == '.' && a[x + 1][y] == 0) {
    a[x + 1][y] = a[x][y];
    pos.push(x * 1000 + 1000 + y);
    dfs(x + 1, y);
  }
  if (x - 1 >= 1 && c[x - 1][y] == '.' && a[x - 1][y] == 0) {
    a[x - 1][y] = a[x][y];
    pos.push(x * 1000 - 1000 + y);
    dfs(x - 1, y);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  char cc;
  for (int i = 1; i <= h; ++i) {
    // cin >> cc;
    for (int j = 1; j <= w; ++j)
      cin >> c[i][j];
  }

  pos.push(ch * 1000 + cw);
  a[ch][cw] = 1;
  int px, py;
  while (!pos.empty()) {
    px = pos.front();
    pos.pop();
    py = px % 1000;
    px = px / 1000;
    if (px == dh && py == dw) {
      break;
    }
    dfs(px, py);
    for (int i = (px - 2 >= 1 ? px - 2 : 1); i <= (px + 2 <= h ? px + 2 : h);
         ++i)
      for (int j = (py - 2 >= 1 ? py - 2 : 1); j <= (py + 2 <= w ? py + 2 : w);
           ++j) {
        if (c[i][j] == '.' && a[i][j] == 0) {
          a[i][j] = a[px][py] + 1;
          pos.push(i * 1000 + j);
        }
      }
  }
  cout << a[dh][dw] - 1 << "\\n";
  return 0;
}