Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

int field[1000][1000];

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '#') {
        field[i][j] = -1;
      } else {
        field[i][j] = 0;
      }
    }
  }

  // rep(i, h) { rep(j, w) cout << field[i][j] << " "; cout << endl; }

  queue<P> que[2];
  que[0].push(make_pair(dh, dw));
  que[1].push(make_pair(dh, dw));
  field[dh][dw] = 1;
  int phase = 1;
  int currentY, currentX;
  while (!que[0].empty() || !que[1].empty()) {

    int queLength = que[0].size();

    while (!que[0].empty()) {
      currentY = que[0].front().first;
      currentX = que[0].front().second;
      que[0].pop();
      if (currentX > 0) {
        // left
        if (field[currentY][currentX - 1] == 0) {
          field[currentY][currentX - 1] = phase;
          que[0].push(make_pair(currentY, currentX - 1));
          que[1].push(make_pair(currentY, currentX - 1));
          queLength++;
        }
      }
      if (currentX + 1 < w) {
        // right
        if (field[currentY][currentX + 1] == 0) {
          field[currentY][currentX + 1] = phase;
          que[0].push(make_pair(currentY, currentX + 1));
          que[1].push(make_pair(currentY, currentX + 1));
          queLength++;
        }
      }
      if (currentY > 0) {
        // up
        if (field[currentY - 1][currentX] == 0) {
          field[currentY - 1][currentX] = phase;
          que[0].push(make_pair(currentY - 1, currentX));
          que[1].push(make_pair(currentY - 1, currentX));
          queLength++;
        }
      }
      if (currentY + 1 < h) {
        // down
        if (field[currentY + 1][currentX] == 0) {
          field[currentY + 1][currentX] = phase;
          que[0].push(make_pair(currentY + 1, currentX));
          que[1].push(make_pair(currentY + 1, currentX));
          queLength++;
        }
      }
    }

    phase++;
    rep(_, queLength) {
      int y = que[1].front().first, x = que[1].front().second;
      que[1].pop();
      for (int i = -2; i <= 2; i++)
        for (int j = -2; j <= 2; j++) {
          int nextY = y + i, nextX = x + j;
          // cout << nextY << " " << nextX << endl;
          if (nextY >= 0 && nextY < h && nextX >= 0 && nextX < w) {
            if (field[nextY][nextX] == 0) {
              // cout << "pushed" << endl;
              field[nextY][nextX] = phase;
              que[0].push(make_pair(nextY, nextX));
              que[1].push(make_pair(nextY, nextX));
            }
          }
        }
    }
    // rep(i, h) { rep(j, w) cout << field[i][j] << " "; cout << endl; } cout <<
    // endl;
  }

  cout << field[ch][cw] - 1 << endl;
  return 0;
}
/*
 */


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

int field[1000][1000];
struct elem {
  int x, y, d;
  elem(int i, int j, int k) {
    y = i;
    x = j;
    d = k;
  }
};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '#') {
        field[i][j] = -1;
      } else {
        field[i][j] = 0;
      }
    }
  }

  // rep(i, h) { rep(j, w) cout << field[i][j] << " "; cout << endl; }

  queue<elem> que[2];
  que[0].push(elem(dh, dw, 1));
  int queid = 0;
  while (!que[0].empty() || !que[1].empty()) {
    while (!que[queid].empty()) {
      int currentY = que[queid].front().y, currentX = que[queid].front().x,
          d = que[queid].front().d;
      que[queid].pop();
      if (field[currentY][currentX] >= 0) {
        if (field[currentY][currentX] > d || field[currentY][currentX] == 0) {
          field[currentY][currentX] = d;
        }
        for (int i = -2; i <= 2; i++)
          for (int j = -2; j <= 2; j++) {
            // cout << i << " " << j << endl;
            int nextY = currentY + i, nextX = currentX + j;
            if (nextY >= 0 && nextY < h && nextX >= 0 && nextX < w) {
              if (field[nextY][nextX] != -1 &&
                  (field[nextY][nextX] > d + 1 || field[nextY][nextX] == 0)) {
                if (field[nextY][nextX] == 0)
                  que[(queid + 1) % 2].push(elem(nextY, nextX, d + 1));
                field[nextY][nextX] = d + 1;
              }
            }
          }
        if (currentX > 0) {
          // left
          if (field[currentY][currentX - 1] != -1 &&
              (field[currentY][currentX - 1] > d ||
               field[currentY][currentX - 1] == 0)) {
            que[queid].push(elem(currentY, currentX - 1, d));
          }
        }
        if (currentX + 1 < w) {
          // right
          if (field[currentY][currentX + 1] != -1 &&
              (field[currentY][currentX + 1] > d ||
               field[currentY][currentX + 1] == 0)) {
            que[queid].push(elem(currentY, currentX + 1, d));
          }
        }
        if (currentY > 0) {
          // up
          if (field[currentY - 1][currentX] != -1 &&
              (field[currentY - 1][currentX] > d ||
               field[currentY - 1][currentX] == 0)) {
            que[queid].push(elem(currentY - 1, currentX, d));
          }
        }
        if (currentY + 1 < h) {
          // down
          if (field[currentY + 1][currentX] != -1 &&
              (field[currentY + 1][currentX] > d ||
               field[currentY + 1][currentX] == 0)) {
            que[queid].push(elem(currentY + 1, currentX, d));
          }
        }
      }
      // rep(i, h) { rep(j, w) cout << field[i][j] << " "; cout << endl; } cout
      // << endl;
    }
    queid = (queid + 1) % 2;
  }

  cout << field[ch][cw] - 1 << endl;
  return 0;
}
/*
 */