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

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define MAXH 1001
#define MAXW 1001
queue<pair<ll, ll>> q1;
queue<pair<ll, ll>> q2;
bool wall[MAXH][MAXW];
int main() {
  int count[MAXH][MAXW];
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {

      count[i][j] = 1000000;
    }
  }
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {
      if (s[j - 1] == '#') {
        wall[i][j] = true;
      } else {
        wall[i][j] = false;
      }
    }
  }
  bool ok = false;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  q1.push(make_pair(ch, cw));
  count[ch][cw] = 0;
  while (q1.size() || q2.size()) {
    pair<ll, ll> p;
    if (q1.size()) {
      p = q1.front();
      if ((p.first == dh) && (p.second == dw)) {
        ok = true;
      }
      q1.pop();
    } else {
      p = q2.front();
      if ((p.first == dh) && (p.second == dw)) {
        ok = true;
      }
      q2.pop();
    }
    for (int i = 0; i < 4; i++) {
      int nextx = p.first + dx[i];
      int nexty = p.second + dy[i];
      if ((1 <= nextx) && (nextx <= h) && (1 <= nexty) && (nexty <= w) &&
          (!wall[nextx][nexty])) {
        if (count[nextx][nexty] > count[p.first][p.second]) {
          count[nextx][nexty] = count[p.first][p.second];
          q1.push(make_pair(nextx, nexty));
        }
      }
    }
    for (int x = -2; x <= 2; x++) {
      for (int y = -2; y <= 2; y++) {
        if (!(((x == 1) && (y == 0)) || ((x == 0) && (y == 1)) ||
              ((x == -1) && (y == 0)) || ((x == 0) && (y == -1)) ||
              ((x == 0) && (y == 0)))) {
          int nextx = p.first + x;
          int nexty = p.second + y;
          if ((1 <= nextx) && (nextx <= h) && (1 <= nexty) && (nexty <= w) &&
              (!wall[nextx][nexty])) {
            if (count[nextx][nexty] > count[p.first][p.second] + 1) {
              count[nextx][nexty] = count[p.first][p.second] + 1;
              q2.push(make_pair(nextx, nexty));
            }
          }
        }
      }
    }
  }
  if (ok) {
    cout << count[dh][dw] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define MAXH 1001
#define MAXW 1001
queue<pair<ll, ll>> q;
bool wall[MAXH][MAXW];
int main() {
  int count[MAXH][MAXW];
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {

      count[i][j] = 1000000;
    }
  }
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {
      if (s[j - 1] == '#') {
        wall[i][j] = true;
      } else {
        wall[i][j] = false;
      }
    }
  }
  bool ok = false;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  q.push(make_pair(ch, cw));
  count[ch][cw] = 0;
  while (q.size()) {
    pair<ll, ll> p = q.front();
    if ((p.first == dh) && (p.second == dw)) {
      ok = true;
    }
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nextx = p.first + dx[i];
      int nexty = p.second + dy[i];
      if ((1 <= nextx) && (nextx <= h) && (1 <= nexty) && (nexty <= w) &&
          (!wall[nextx][nexty])) {
        if (count[nextx][nexty] > count[p.first][p.second]) {
          count[nextx][nexty] = count[p.first][p.second];
          q.push(make_pair(nextx, nexty));
        }
      }
    }
    for (int x = -2; x <= 2; x++) {
      for (int y = -2; y <= 2; y++) {
        if (!(((x == 1) && (y == 0)) || ((x == 0) && (y == 1)) ||
              ((x == -1) && (y == 0)) || ((x == 0) && (y == -1)) ||
              ((x == 0) && (y == 0)))) {
          int nextx = p.first + x;
          int nexty = p.second + y;
          if ((1 <= nextx) && (nextx <= h) && (1 <= nexty) && (nexty <= w) &&
              (!wall[nextx][nexty])) {
            if (count[nextx][nexty] > count[p.first][p.second] + 1) {
              count[nextx][nexty] = count[p.first][p.second] + 1;
              q.push(make_pair(nextx, nexty));
            }
          }
        }
      }
    }
  }
  if (ok) {
    cout << count[dh][dw] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}